Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
45eb613
Initial commit of fork
sidz Jan 28, 2024
37da253
fix stub filename
sidz Jan 28, 2024
1613b9f
Fix ClassRegistryInit doesn't work with ClassConstFetch AST
sidz Jan 28, 2024
3f89598
Fix ClassRegistryInit doesn't work with ClassConstFetch AST
sidz Jan 28, 2024
bd6b9ca
Merge pull request #1 from sidz/fix-class-registry-to-be-able-to-work…
sidz Jan 28, 2024
8121dde
Mark as Default type if variable is passed into `ClassRegistr::init` …
sidz Jan 29, 2024
467dd97
Merge pull request #3 from sidz/mark-as-default-if-varuable-passed-in…
sidz Jan 29, 2024
e8a3efe
Fix error occurred when not a string is passed into `ClassRegistry::i…
sidz Jan 29, 2024
2fe3af0
Merge pull request #4 from sidz/fix-error-occured-if-not-a-string-pas…
sidz Jan 29, 2024
30f04ce
Teach see a difference between Component and Model especially when bo…
sidz Dec 24, 2023
99d7bfd
Merge pull request #2 from sidz/teach-to-see-a-different-between-mode…
sidz Jan 29, 2024
66db9e8
Add Router.stub and allow to accept string as $url argument
sidz Jan 29, 2024
fa4edd9
Merge pull request #5 from sidz/add-router-stub
sidz Jan 29, 2024
16be682
Add Model.stub and define what find('count') returns
sidz Apr 29, 2024
58bf6ae
Merge pull request #6 from sidz/add-model-stub-with-find-count-method
sidz Apr 29, 2024
05001cf
Rename Utility.stub to ClassRegistry.stub and move to right namespace
sidz Apr 29, 2024
ed5c00a
Merge pull request #7 from sidz/rename-utility-stub-to-class-registry
sidz Apr 29, 2024
20b1b9c
add basics.php stub
sidz Apr 29, 2024
2d96028
Merge pull request #8 from sidz/provide-stubs-for-basics-php
sidz May 1, 2024
3cb307e
Support PHPStan 2
sidz Jan 15, 2025
21673aa
Merge pull request #9 from sidz/use-phpstan-2
sidz Jan 15, 2025
1f41412
fix requirements
sidz Jan 11, 2026
239b911
Use latest actions/checkout and do not disable block-insecure
sidz Jan 11, 2026
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
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v6

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
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.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ PHPStan extensions to help test CakePHP 2 projects with PHPStan
## Requirements

* PHP 7.4
* phpstan/phpstan 1.9+
* phpstan/phpstan 2.x
* cakephp/cakephp 2.x

## Installation

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
23 changes: 13 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"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": {
"php": "^7.4",
"phpstan/phpstan": "^1.9"
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"cakephp/cakephp": "^2.10.24",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.6",
"nunomaduro/phpinsights": "^2.7"
"nunomaduro/phpinsights": "^2.7",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^9.6"
},
"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 All @@ -63,6 +62,10 @@
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true,
"audit": {
"block-insecure": false
}
}
}
20 changes: 12 additions & 8 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ parameters:
schemaPaths:
- app/Config/Schema/*.php
stubFiles:
- stubs/Utility.php
- stubs/Model/Model.stub
- stubs/Routing/Router.stub
- stubs/Utility/ClassRegistry.stub
bootstrapFiles:
- stubs/basics.php
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
38 changes: 24 additions & 14 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,28 @@ 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)) {
return $this->getDefaultType();
}
if ($this->reflectionProvider->hasClass($arg1)) {
return new ObjectType($arg1);
}
if ($this->schemaService->hasTable(Inflector::tableize($arg1))) {
return new ObjectType('Model');
$type = $scope->getType($methodCall->getArgs()[0]->value);

foreach ($type->getConstantStrings() as $constantString) {
$value = $constantString->getValue();

if ($this->reflectionProvider->hasClass($value)) {
return new ObjectType($value);
}

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(),
]);
}
}
Loading