Skip to content

Commit 8e2abfe

Browse files
Initial commit. (#1)
1 parent ac566f0 commit 8e2abfe

32 files changed

+1245
-78
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
<p align="center">
2-
<a href="https://github.com/yii2-extensions/template" target="_blank">
2+
<a href="https://github.com/yii2-extensions/phpstan" target="_blank">
33
<img src="https://www.yiiframework.com/image/yii_logo_light.svg" height="100px;">
44
</a>
5-
<h1 align="center">Yii2-Template.</h1>
5+
<h1 align="center">PHPStan extension for Yii2</h1>
66
<br>
77
</p>
88

99
[![php-version](https://img.shields.io/badge/PHP-%3E%3D8.1-787CB5)](https://www.php.net/releases/8.1/en.php)
1010
[![yii2-version](https://img.shields.io/badge/yii2%20version-2.2-blue)](https://github.com/yiisoft/yii2/tree/2.2)
11-
[![build](https://github.com/yii2-extensions/template/actions/workflows/build.yml/badge.svg)](https://github.com/yii2-extensions/template/actions/workflows/build.yml)
12-
[![codecov](https://codecov.io/gh/yii2-extensions/template/branch/main/graph/badge.svg?token=MF0XUGVLYC)](https://codecov.io/gh/yii2-extensions/template)
13-
[![static analysis](https://github.com/yii2-extensions/template/actions/workflows/static.yml/badge.svg)](https://github.com/yii2-extensions/template/actions/workflows/static.yml)
14-
[![StyleCI](https://github.styleci.io/repos/698621511/shield?branch=main)](https://github.styleci.io/repos/698621511?branch=main)
11+
[![build](https://github.com/yii2-extensions/phpstan/actions/workflows/build.yml/badge.svg)](https://github.com/yii2-extensions/phpstan/actions/workflows/build.yml)
12+
[![codecov](https://codecov.io/gh/yii2-extensions/phpstan/branch/main/graph/badge.svg?token=MF0XUGVLYC)](https://codecov.io/gh/yii2-extensions/phpstan)
13+
[![static analysis](https://github.com/yii2-extensions/phpstan/actions/workflows/static.yml/badge.svg)](https://github.com/yii2-extensions/phpstan/actions/workflows/static.yml)
14+
[![StyleCI](https://github.styleci.io/repos/701347895/shield?branch=main)](https://github.styleci.io/repos/701347895?branch=main)
1515

1616
## Requirements
1717

1818
The minimun version of `PHP` required by this package is `PHP 8.1`.
1919

2020
For install this package, you need [composer](https://getcomposer.org/).
2121

22-
## Usage
23-
24-
[Check the documentation docs](/docs/README.md) to learn about usage.
25-
2622
## Testing
2723

2824
[Check the documentation testing](/docs/testing.md) to learn about testing.
@@ -34,3 +30,7 @@ For install this package, you need [composer](https://getcomposer.org/).
3430
## License
3531

3632
The MIT License. Please see [License File](LICENSE.md) for more information.
33+
34+
## Fork
35+
36+
This package is a fork of [proget-hq/phpstan-yii2](https://github.com/proget-hq/phpstan-yii2) with some corrections.

composer-require-checker.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"symbol-whitelist": [
3+
"PHPStan\\Analyser\\OutOfClassScope",
4+
"PHPStan\\Analyser\\Scope",
5+
"PHPStan\\Reflection\\Annotations\\AnnotationsPropertiesClassReflectionExtension",
6+
"PHPStan\\Reflection\\ClassReflection",
7+
"PHPStan\\Reflection\\Dummy\\DummyPropertyReflection",
8+
"PHPStan\\Reflection\\MethodReflection",
9+
"PHPStan\\Reflection\\MethodsClassReflectionExtension",
10+
"PHPStan\\Reflection\\ParametersAcceptorSelector",
11+
"PHPStan\\Reflection\\PropertiesClassReflectionExtension",
12+
"PHPStan\\Reflection\\PropertyReflection",
13+
"PHPStan\\Reflection\\ReflectionProvider",
14+
"PHPStan\\ShouldNotHappenException",
15+
"PHPStan\\TrinaryLogic",
16+
"PHPStan\\Type\\ArrayType",
17+
"PHPStan\\Type\\Constant\\ConstantBooleanType",
18+
"PHPStan\\Type\\Constant\\ConstantStringType",
19+
"PHPStan\\Type\\DynamicMethodReturnTypeExtension",
20+
"PHPStan\\Type\\DynamicStaticMethodReturnTypeExtension",
21+
"PHPStan\\Type\\ErrorType",
22+
"PHPStan\\Type\\IntegerType",
23+
"PHPStan\\Type\\MixedType",
24+
"PHPStan\\Type\\NullType",
25+
"PHPStan\\Type\\ObjectType",
26+
"PHPStan\\Type\\StringType",
27+
"PHPStan\\Type\\ThisType",
28+
"PHPStan\\Type\\Type",
29+
"PHPStan\\Type\\TypeCombinator",
30+
"PHPStan\\Type\\UnionType",
31+
"PHPStan\\Type\\VerbosityLevel"
32+
]
33+
}

composer.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
{
2-
"name": "yii2/template",
2+
"name": "yii2/phpstan",
33
"type": "library",
4-
"description": "_____",
4+
"description": "PHPStan extension for Yii 2",
55
"keywords": [
6-
"_____"
6+
"yii2",
7+
"phpstan"
78
],
89
"license": "mit",
910
"minimum-stability": "dev",
1011
"prefer-stable": true,
1112
"require": {
1213
"php": ">=8.1",
14+
"nikic/php-parser": "^4.1.0",
15+
"phpstan/phpstan": "^1.0",
1316
"yiisoft/yii2": "^2.2"
1417
},
1518
"require-dev": {
1619
"maglnet/composer-require-checker": "^4.6",
17-
"phpunit/phpunit": "^10.2",
18-
"proget-hq/phpstan-yii2": "^0.8.0"
20+
"phpstan/phpstan-phpunit": "^1.0",
21+
"phpunit/phpunit": "^10.2"
1922
},
2023
"autoload": {
2124
"psr-4": {
22-
"yii\\template\\": "src"
25+
"yii\\phpstan\\": "src"
2326
}
2427
},
2528
"autoload-dev": {
2629
"psr-4": {
27-
"yii\\template\\tests\\": "tests"
30+
"yii\\phpstan\\tests\\": "tests"
2831
}
2932
},
3033
"extra": {
@@ -40,7 +43,6 @@
4043
},
4144
"scripts": {
4245
"check-dependencies": "composer-require-checker",
43-
"mutation": "roave-infection-static-analysis-plugin",
4446
"phpstan": "phpstan",
4547
"test": "phpunit"
4648
},

extension.neon

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
parameters:
2+
yii2:
3+
config_path: null
4+
stubFiles:
5+
- stubs/BaseYii.stub
6+
7+
parametersSchema:
8+
yii2: structure([
9+
config_path: schema(string(), nullable())
10+
])
11+
12+
services:
13+
-
14+
class: yii\phpstan\Reflection\ApplicationPropertiesClassReflectionExtension
15+
tags: [phpstan.broker.propertiesClassReflectionExtension]
16+
-
17+
class: yii\phpstan\Reflection\RequestMethodsClassReflectionExtension
18+
tags: [phpstan.broker.methodsClassReflectionExtension]
19+
-
20+
class: yii\phpstan\Reflection\RequestPropertiesClassReflectionExtension
21+
tags: [phpstan.broker.propertiesClassReflectionExtension]
22+
-
23+
class: yii\phpstan\Reflection\ResponsePropertiesClassReflectionExtension
24+
tags: [phpstan.broker.propertiesClassReflectionExtension]
25+
-
26+
class: yii\phpstan\Reflection\UserPropertiesClassReflectionExtension
27+
tags: [phpstan.broker.propertiesClassReflectionExtension]
28+
-
29+
class: yii\phpstan\Type\ActiveQueryDynamicMethodReturnTypeExtension
30+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
31+
-
32+
class: yii\phpstan\Type\ActiveRecordDynamicMethodReturnTypeExtension
33+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
34+
-
35+
class: yii\phpstan\Type\HeaderCollectionDynamicMethodReturnTypeExtension
36+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
37+
-
38+
class: yii\phpstan\Type\ActiveRecordDynamicStaticMethodReturnTypeExtension
39+
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]
40+
-
41+
class: yii\phpstan\Type\ContainerDynamicMethodReturnTypeExtension
42+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
43+
44+
- yii\phpstan\ServiceMap(%yii2.config_path%)

phpstan.neon

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
includes:
2-
- vendor/proget-hq/phpstan-yii2/extension.neon
2+
- extension.neon
3+
- vendor/phpstan/phpstan-phpunit/extension.neon
4+
- vendor/phpstan/phpstan-phpunit/rules.neon
5+
36
parameters:
4-
dynamicConstantNames:
5-
- YII_DEBUG
6-
- YII_ENV
7-
- YII_ENV_DEV
8-
- YII_ENV_PROD
9-
- YII_ENV_TEST
7+
ignoreErrors:
8+
- '#Calling PHPStan\\Reflection\\Annotations\\AnnotationsPropertiesClassReflectionExtension\:\:(has|get)Property\(\) is not covered.+#'
9+
- '#Creating new PHPStan\\Reflection\\Dummy\\DummyPropertyReflection is not covered.+#'
1010

11-
level: 2
11+
level: 6
1212

1313
paths:
1414
- src
@@ -17,5 +17,4 @@ parameters:
1717
- vendor/yiisoft/yii2/Yii.php
1818

1919
yii2:
20-
config_path: %currentWorkingDirectory%/phpstan-yii-config.php
21-
20+
config_path: tests/assets/yii-config-valid.php

phpunit.xml.dist

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
5-
bootstrap="vendor/autoload.php"
6-
cacheDirectory=".phpunit.cache"
7-
colors="true"
8-
executionOrder="depends,defects"
9-
failOnRisky="true"
10-
failOnWarning="true"
11-
stopOnFailure="false"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
cacheDirectory=".phpunit.cache"
7+
colors="true"
8+
executionOrder="depends,defects"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
stopOnFailure="false"
1212
>
13-
<testsuites>
14-
<testsuite name="Template">
15-
<directory>tests</directory>
16-
</testsuite>
17-
</testsuites>
13+
<testsuites>
14+
<testsuite name="Yii2-PHPstan">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
1818

19-
<source>
20-
<include>
21-
<directory suffix=".php">./src</directory>
22-
</include>
23-
</source>
19+
<source>
20+
<include>
21+
<directory suffix=".php">./src</directory>
22+
</include>
23+
</source>
2424
</phpunit>

src/Example.php

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace yii\phpstan\Reflection;
6+
7+
use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension;
8+
use PHPStan\Reflection\ClassReflection;
9+
use PHPStan\Reflection\Dummy\DummyPropertyReflection;
10+
use PHPStan\Reflection\PropertiesClassReflectionExtension;
11+
use PHPStan\Reflection\PropertyReflection;
12+
use PHPStan\Reflection\ReflectionProvider;
13+
use PHPStan\Type\ObjectType;
14+
use yii\base\Application as BaseApplication;
15+
use yii\phpstan\ServiceMap;
16+
use yii\web\Application as WebApplication;
17+
18+
final class ApplicationPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
19+
{
20+
/**
21+
* @var AnnotationsPropertiesClassReflectionExtension
22+
*/
23+
private $annotationsProperties;
24+
25+
/**
26+
* @var ServiceMap
27+
*/
28+
private $serviceMap;
29+
30+
/**
31+
* @var \PHPStan\Reflection\ReflectionProvider
32+
*/
33+
private $reflectionProvider;
34+
35+
public function __construct(
36+
AnnotationsPropertiesClassReflectionExtension $annotationsProperties,
37+
ReflectionProvider $reflectionProvider,
38+
ServiceMap $serviceMap
39+
) {
40+
$this->annotationsProperties = $annotationsProperties;
41+
$this->serviceMap = $serviceMap;
42+
$this->reflectionProvider = $reflectionProvider;
43+
}
44+
45+
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
46+
{
47+
if ($classReflection->getName() !== BaseApplication::class && !$classReflection->isSubclassOf(BaseApplication::class)) {
48+
return false;
49+
}
50+
51+
if ($classReflection->getName() !== WebApplication::class) {
52+
$classReflection = $this->reflectionProvider->getClass(WebApplication::class);
53+
}
54+
55+
return $classReflection->hasNativeProperty($propertyName)
56+
|| $this->annotationsProperties->hasProperty($classReflection, $propertyName)
57+
|| $this->serviceMap->getComponentClassById($propertyName);
58+
}
59+
60+
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
61+
{
62+
if ($classReflection->getName() !== WebApplication::class) {
63+
$classReflection = $this->reflectionProvider->getClass(WebApplication::class);
64+
}
65+
66+
if (null !== $componentClass = $this->serviceMap->getComponentClassById($propertyName)) {
67+
return new ComponentPropertyReflection(new DummyPropertyReflection(), new ObjectType($componentClass));
68+
}
69+
70+
if ($classReflection->hasNativeProperty($propertyName)) {
71+
return $classReflection->getNativeProperty($propertyName);
72+
}
73+
74+
return $this->annotationsProperties->getProperty($classReflection, $propertyName);
75+
}
76+
}

0 commit comments

Comments
 (0)