Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Utility.stub to ClassRegistry.stub #35

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
MIT License
The MIT License
---------------

Copyright (c) 2022 Andrew Riddlestone
Copyright (c) 2023 Oleg Zhulnev (https://github.com/sidz)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ PHPStan extensions to help test CakePHP 2 projects with PHPStan

Installation is best done through composer:
```shell
composer require --dev ariddlestone/phpstan-cakephp2
composer require --dev sidz/phpstan-cakephp2
```

You will need to make sure the extension is included in your phpstan config:
```yaml
# phpstan.neon
includes:
- vendor/ariddlestone/phpstan-cakephp2/extension.neon
- vendor/sidz/phpstan-cakephp2/extension.neon
```

If you have behavior classes in odd locations (perhaps in a vendor directory) you will need to add those locations to
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "ariddlestone/phpstan-cakephp2",
"name": "sidz/phpstan-cakephp2",
"description": "An extension to help test CakePHP 2 projects with PHPStan",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Andrew Riddlestone",
"email": "andrew.riddlestone@gmail.com"
"name": "Oleg Zhulnev",
"email": "plbsid@gmail.com"
}
],
"require": {
Expand All @@ -21,12 +21,12 @@
},
"autoload": {
"psr-4": {
"ARiddlestone\\PHPStanCakePHP2\\": "src/"
"PHPStanCakePHP2\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ARiddlestone\\PHPStanCakePHP2\\Test\\": "tests/"
"PHPStanCakePHP2\\Test\\": "tests/"
},
"classmap": [
"tests/Feature/classes",
Expand All @@ -42,7 +42,6 @@
]
},
"scripts": {
"extract-phpstan": "phar extract -f vendor/phpstan/phpstan/phpstan.phar phpstan",
"phpinsights": "phpinsights analyse --no-interaction --ansi --config-path=phpinsights.php --summary src",
"phpinsights-github": "phpinsights analyse --no-interaction --ansi --config-path=phpinsights.php --format=github-action src",
"phpstan": "phpstan",
Expand Down
18 changes: 10 additions & 8 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ parameters:
schemaPaths:
- app/Config/Schema/*.php
stubFiles:
- stubs/Utility.php
- stubs/Model/Model.stub
- stubs/Routing/Router.stub
- stubs/Utility/ClassRegistry.stub
services:
- class: ARiddlestone\PHPStanCakePHP2\ClassComponentsExtension
- class: PHPStanCakePHP2\ClassComponentsExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension
- class: ARiddlestone\PHPStanCakePHP2\ClassModelsExtension
- class: PHPStanCakePHP2\ClassModelsExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension
- class: ARiddlestone\PHPStanCakePHP2\ClassRegistryInitExtension
- class: PHPStanCakePHP2\ClassRegistryInitExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
- class: ARiddlestone\PHPStanCakePHP2\ClassTasksExtension
- class: PHPStanCakePHP2\ClassTasksExtension
tags:
- phpstan.broker.propertiesClassReflectionExtension
- class: ARiddlestone\PHPStanCakePHP2\ModelBehaviorsExtension
- class: PHPStanCakePHP2\ModelBehaviorsExtension
arguments:
behaviorPaths: %ModelBehaviorsExtension.behaviorPaths%
tags:
- phpstan.broker.methodsClassReflectionExtension
- class: ARiddlestone\PHPStanCakePHP2\Service\SchemaService
- class: PHPStanCakePHP2\Service\SchemaService
arguments:
schemaPaths: %SchemaService.schemaPaths%
- class: ARiddlestone\PHPStanCakePHP2\LoadComponentOnFlyMethodReturnTypeExtension
- class: PHPStanCakePHP2\LoadComponentOnFlyMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
parametersSchema:
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
Expand Down
94 changes: 85 additions & 9 deletions src/ClassComponentsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,105 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

final class ClassComponentsExtension extends ClassPropertiesExtension
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;

final class ClassComponentsExtension implements PropertiesClassReflectionExtension
{
protected function getPropertyParentClassName(): string
private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
return 'Component';
$this->reflectionProvider = $reflectionProvider;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!array_filter($this->getContainingClassNames(), [$classReflection, 'is'])) {
return false;
}

$isDefinedInComponentsProperty = (bool) array_filter(
$this->getDefinedComponentsAsList($classReflection),
static fn (string $componentName): bool => $componentName === $propertyName
);

if (!$isDefinedInComponentsProperty) {
return false;
}

$propertyClassName = $this->getClassNameFromPropertyName($propertyName);

return $this->reflectionProvider->hasClass($propertyClassName)
&& $this->reflectionProvider->getClass($propertyClassName)
->is('Component');
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
return new PublicReadOnlyPropertyReflection(
$this->getClassNameFromPropertyName($propertyName),
$classReflection
);
}

/**
* @return array<string>
*/
protected function getContainingClassNames(): array
private function getContainingClassNames(): array
{
return [
'Controller',
'Component',
];
}

protected function getClassNameFromPropertyName(
string $propertyName
): string {
return $propertyName . 'Component';
private function getClassNameFromPropertyName(string $propertyName): string
{
return str_contains($propertyName, 'Component') ? $propertyName : $propertyName . 'Component';
}

/**
* @return list<string>
*/
private function getDefinedComponentsAsList(ClassReflection $classReflection): array
{
$definedComponents = [];

foreach (array_merge([$classReflection], $classReflection->getParents()) as $class) {
if (!$class->hasProperty('components')) {
continue;
}

$defaultValue = $class->getNativeProperty('components')
->getNativeReflection()
->getDefaultValueExpression();

if (!$defaultValue instanceof Array_) {
continue;
}

foreach ($defaultValue->items as $item) {
if ($item->value instanceof String_) {
$definedComponents[] = $item->value->value;

continue;
}

if ($item->value instanceof ClassConstFetch && $item->value->class instanceof FullyQualified) {
$definedComponents[] = $item->value->class->toString();
}
}
}

return $definedComponents;
}
}
2 changes: 1 addition & 1 deletion src/ClassModelsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

/**
* Adds {@link Model}s as properties to {@link Shell}s.
Expand Down
5 changes: 2 additions & 3 deletions src/ClassPropertiesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;

abstract class ClassPropertiesExtension implements
PropertiesClassReflectionExtension
abstract class ClassPropertiesExtension implements PropertiesClassReflectionExtension
{
private ReflectionProvider $reflectionProvider;

Expand Down
2 changes: 1 addition & 1 deletion src/ClassReflectionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

use Exception;
use PHPStan\Reflection\ClassReflection;
Expand Down
32 changes: 22 additions & 10 deletions src/ClassRegistryInitExtension.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?php

namespace ARiddlestone\PHPStanCakePHP2;
declare(strict_types=1);

use ARiddlestone\PHPStanCakePHP2\Service\SchemaService;
namespace PHPStanCakePHP2;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStanCakePHP2\Service\SchemaService;
use Inflector;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node\Expr\StaticCall;
Expand Down Expand Up @@ -42,26 +50,30 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
$arg1 = $methodCall->getArgs()[0]->value;
$evaluator = new ConstExprEvaluator();
$arg1 = $evaluator->evaluateSilently($arg1);
if (! is_string($arg1)) {
$argumentType = $scope->getType($methodCall->getArgs()[0]->value);

if (!$argumentType instanceof ConstantStringType) {
return $this->getDefaultType();
}
if ($this->reflectionProvider->hasClass($arg1)) {
return new ObjectType($arg1);

$value = $argumentType->getValue();

if ($this->reflectionProvider->hasClass($value)) {
return new ObjectType($value);
}
if ($this->schemaService->hasTable(Inflector::tableize($arg1))) {

if ($this->schemaService->hasTable(Inflector::tableize($value))) {
return new ObjectType('Model');
}

return $this->getDefaultType();
}

private function getDefaultType(): Type
{
return new UnionType([
new BooleanType(),
new ObjectWithoutClassType()
new ObjectWithoutClassType(),
]);
}
}
2 changes: 1 addition & 1 deletion src/ClassTasksExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

/**
* Adds {@link Model}s as properties to {@link Shell}s.
Expand Down
2 changes: 1 addition & 1 deletion src/LoadComponentOnFlyMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

use Component;
use PhpParser\Node\Expr\MethodCall;
Expand Down
7 changes: 2 additions & 5 deletions src/ModelBehaviorMethodExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace ARiddlestone\PHPStanCakePHP2;
namespace PHPStanCakePHP2;

use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -14,10 +14,7 @@

final class ModelBehaviorMethodExtractor
{
/**
* @var ClassReflection
*/
private $classReflection;
private ClassReflection $classReflection;

public function __construct(ClassReflection $classReflection)
{
Expand Down
Loading
Loading