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

Fix class registry to be able to work with class const fetch #33

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
16 changes: 8 additions & 8 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ parameters:
schemaPaths:
- app/Config/Schema/*.php
stubFiles:
- stubs/Utility.php
- stubs/Utility.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
2 changes: 1 addition & 1 deletion src/ClassComponentsExtension.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;

final class ClassComponentsExtension extends ClassPropertiesExtension
{
Expand Down
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
19 changes: 15 additions & 4 deletions src/ClassRegistryInitExtension.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

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

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

use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PHPStanCakePHP2\Service\SchemaService;
use Inflector;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node\Expr\StaticCall;
Expand Down Expand Up @@ -42,9 +47,15 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo

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

if ($value instanceof ClassConstFetch && $value->class instanceof Name\FullyQualified) {
$value = new String_($value->class->toString());
}

$arg1 = $evaluator->evaluateSilently($value);

if (! is_string($arg1)) {
return $this->getDefaultType();
}
Expand Down
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
7 changes: 2 additions & 5 deletions src/ModelBehaviorMethodWrapper.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\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -18,10 +18,7 @@
*/
final class ModelBehaviorMethodWrapper implements MethodReflection
{
/**
* @var MethodReflection
*/
private $wrappedMethod;
private MethodReflection $wrappedMethod;

public function __construct(MethodReflection $wrappedMethod)
{
Expand Down
14 changes: 6 additions & 8 deletions src/ModelBehaviorsExtension.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 All @@ -15,20 +15,17 @@
*/
final class ModelBehaviorsExtension implements MethodsClassReflectionExtension
{
/**
* @var ReflectionProvider
*/
private $reflectionProvider;
private ReflectionProvider $reflectionProvider;

/**
* @var array<string>
*/
private $behaviorPaths;
private array $behaviorPaths;

/**
* @var array<MethodReflection>|null
*/
private $behaviorMethods = null;
private ?array $behaviorMethods = null;

/**
* @param array<string> $behaviorPaths
Expand All @@ -54,7 +51,8 @@ public function hasMethod(
array_map(
[$this, 'getMethodReflectionName'],
$this->getBehaviorMethods()
)
),
true
);
}

Expand Down
14 changes: 4 additions & 10 deletions src/PublicReadOnlyPropertyReflection.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\Reflection\ClassReflection;
use PHPStan\Reflection\PropertyReflection;
Expand All @@ -12,15 +12,9 @@

final class PublicReadOnlyPropertyReflection implements PropertyReflection
{
/**
* @var string
*/
private $name;

/**
* @var ClassReflection
*/
private $declaringClass;
private string $name;

private ClassReflection $declaringClass;

public function __construct(string $name, ClassReflection $declaringClass)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Service/SchemaService.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

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

use ARiddlestone\PHPStanCakePHP2\ClassReflectionFinder;
namespace PHPStanCakePHP2\Service;

use PHPStanCakePHP2\ClassReflectionFinder;
use Exception;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -12,8 +14,8 @@
* Identifies schema files, and uses them to provide information about tables
* and columns in the database.
*
* @phpstan-type table_schema mixed

Check failure on line 17 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Disallow mixed type hint] Usage of "mixed" type hint is disallowed.
* @phpstan-type column_schema mixed

Check failure on line 18 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Disallow mixed type hint] Usage of "mixed" type hint is disallowed.
*/
final class SchemaService
{
Expand All @@ -30,7 +32,7 @@
private ?array $tableSchemas = null;

/**
* @param ReflectionProvider $reflectionProvider

Check failure on line 35 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Parameter type hint] Method \PHPStanCakePHP2\Service\SchemaService::__construct() has useless @param annotation for parameter $reflectionProvider.
* @param array<string> $schemaPaths
*/
public function __construct(
Expand All @@ -50,7 +52,7 @@
}

/**
* @param string $table

Check failure on line 55 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Parameter type hint] Method \PHPStanCakePHP2\Service\SchemaService::getTableSchema() has useless @param annotation for parameter $table.
* @return table_schema|null
* @throws Exception
*/
Expand All @@ -67,16 +69,16 @@
*
* @throws Exception
*/
private function getTableSchemas(): array

Check failure on line 72 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Function length] Your function is too long. Currently using 35 lines. Can be up to 20 lines.
{
if (is_array($this->tableSchemas)) {
return $this->tableSchemas;
}
$cakeSchemaPropertyNames = array_map(
function (ReflectionProperty $reflectionProperty) {

Check failure on line 78 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Static closure] Closure not using "$this" should be declared static.
return $reflectionProperty->getName();
},
$this->reflectionProvider->getClass('CakeSchema')->getNativeReflection()->getProperties()

Check failure on line 81 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Line length] Line exceeds maximum limit of 100 characters; contains 101 characters
);
$this->tableSchemas = [];
$classReflectionFinder = new ClassReflectionFinder(
Expand All @@ -91,15 +93,15 @@
);
foreach ($schemaReflections as $schemaReflection) {
$propertyNames = array_map(
function (ReflectionProperty $reflectionProperty) {

Check failure on line 96 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Static closure] Closure not using "$this" should be declared static.
return $reflectionProperty->getName();
},
$schemaReflection->getNativeReflection()
->getProperties(CoreReflectionProperty::IS_PUBLIC)
);
$tableProperties = array_diff($propertyNames, $cakeSchemaPropertyNames);

Check failure on line 102 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Line length] Line exceeds 80 characters; contains 84 characters
$this->tableSchemas += array_intersect_key(
$schemaReflection->getNativeReflection()->getDefaultProperties(),

Check failure on line 104 in src/Service/SchemaService.php

View workflow job for this annotation

GitHub Actions / build

* [Line length] Line exceeds 80 characters; contains 81 characters
array_fill_keys($tableProperties, null)
);
}
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion tests/Feature/ClassRegistryInitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

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

namespace PHPStanCakePHP2\Test\Feature;

use PHPStan\Testing\TypeInferenceTestCase;

Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/ComponentExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

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

namespace PHPStanCakePHP2\Test\Feature;

use PHPStan\Testing\TypeInferenceTestCase;

Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/ControllerExtensionsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

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

namespace PHPStanCakePHP2\Test\Feature;

use PHPStan\Testing\TypeInferenceTestCase;

Expand Down
Loading
Loading