Skip to content

Commit

Permalink
Initial commit. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Oct 6, 2023
1 parent ac566f0 commit 8e2abfe
Show file tree
Hide file tree
Showing 32 changed files with 1,245 additions and 78 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<p align="center">
<a href="https://github.com/yii2-extensions/template" target="_blank">
<a href="https://github.com/yii2-extensions/phpstan" target="_blank">
<img src="https://www.yiiframework.com/image/yii_logo_light.svg" height="100px;">
</a>
<h1 align="center">Yii2-Template.</h1>
<h1 align="center">PHPStan extension for Yii2</h1>
<br>
</p>

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

## Requirements

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

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

## Usage

[Check the documentation docs](/docs/README.md) to learn about usage.

## Testing

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

The MIT License. Please see [License File](LICENSE.md) for more information.

## Fork

This package is a fork of [proget-hq/phpstan-yii2](https://github.com/proget-hq/phpstan-yii2) with some corrections.
33 changes: 33 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"symbol-whitelist": [
"PHPStan\\Analyser\\OutOfClassScope",
"PHPStan\\Analyser\\Scope",
"PHPStan\\Reflection\\Annotations\\AnnotationsPropertiesClassReflectionExtension",
"PHPStan\\Reflection\\ClassReflection",
"PHPStan\\Reflection\\Dummy\\DummyPropertyReflection",
"PHPStan\\Reflection\\MethodReflection",
"PHPStan\\Reflection\\MethodsClassReflectionExtension",
"PHPStan\\Reflection\\ParametersAcceptorSelector",
"PHPStan\\Reflection\\PropertiesClassReflectionExtension",
"PHPStan\\Reflection\\PropertyReflection",
"PHPStan\\Reflection\\ReflectionProvider",
"PHPStan\\ShouldNotHappenException",
"PHPStan\\TrinaryLogic",
"PHPStan\\Type\\ArrayType",
"PHPStan\\Type\\Constant\\ConstantBooleanType",
"PHPStan\\Type\\Constant\\ConstantStringType",
"PHPStan\\Type\\DynamicMethodReturnTypeExtension",
"PHPStan\\Type\\DynamicStaticMethodReturnTypeExtension",
"PHPStan\\Type\\ErrorType",
"PHPStan\\Type\\IntegerType",
"PHPStan\\Type\\MixedType",
"PHPStan\\Type\\NullType",
"PHPStan\\Type\\ObjectType",
"PHPStan\\Type\\StringType",
"PHPStan\\Type\\ThisType",
"PHPStan\\Type\\Type",
"PHPStan\\Type\\TypeCombinator",
"PHPStan\\Type\\UnionType",
"PHPStan\\Type\\VerbosityLevel"
]
}
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
{
"name": "yii2/template",
"name": "yii2/phpstan",
"type": "library",
"description": "_____",
"description": "PHPStan extension for Yii 2",
"keywords": [
"_____"
"yii2",
"phpstan"
],
"license": "mit",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"nikic/php-parser": "^4.1.0",
"phpstan/phpstan": "^1.0",
"yiisoft/yii2": "^2.2"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.6",
"phpunit/phpunit": "^10.2",
"proget-hq/phpstan-yii2": "^0.8.0"
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^10.2"
},
"autoload": {
"psr-4": {
"yii\\template\\": "src"
"yii\\phpstan\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"yii\\template\\tests\\": "tests"
"yii\\phpstan\\tests\\": "tests"
}
},
"extra": {
Expand All @@ -40,7 +43,6 @@
},
"scripts": {
"check-dependencies": "composer-require-checker",
"mutation": "roave-infection-static-analysis-plugin",
"phpstan": "phpstan",
"test": "phpunit"
},
Expand Down
44 changes: 44 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
parameters:
yii2:
config_path: null
stubFiles:
- stubs/BaseYii.stub

parametersSchema:
yii2: structure([
config_path: schema(string(), nullable())
])

services:
-
class: yii\phpstan\Reflection\ApplicationPropertiesClassReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]
-
class: yii\phpstan\Reflection\RequestMethodsClassReflectionExtension
tags: [phpstan.broker.methodsClassReflectionExtension]
-
class: yii\phpstan\Reflection\RequestPropertiesClassReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]
-
class: yii\phpstan\Reflection\ResponsePropertiesClassReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]
-
class: yii\phpstan\Reflection\UserPropertiesClassReflectionExtension
tags: [phpstan.broker.propertiesClassReflectionExtension]
-
class: yii\phpstan\Type\ActiveQueryDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: yii\phpstan\Type\ActiveRecordDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: yii\phpstan\Type\HeaderCollectionDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
-
class: yii\phpstan\Type\ActiveRecordDynamicStaticMethodReturnTypeExtension
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]
-
class: yii\phpstan\Type\ContainerDynamicMethodReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

- yii\phpstan\ServiceMap(%yii2.config_path%)
19 changes: 9 additions & 10 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
includes:
- vendor/proget-hq/phpstan-yii2/extension.neon
- extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
dynamicConstantNames:
- YII_DEBUG
- YII_ENV
- YII_ENV_DEV
- YII_ENV_PROD
- YII_ENV_TEST
ignoreErrors:
- '#Calling PHPStan\\Reflection\\Annotations\\AnnotationsPropertiesClassReflectionExtension\:\:(has|get)Property\(\) is not covered.+#'
- '#Creating new PHPStan\\Reflection\\Dummy\\DummyPropertyReflection is not covered.+#'

level: 2
level: 6

paths:
- src
Expand All @@ -17,5 +17,4 @@ parameters:
- vendor/yiisoft/yii2/Yii.php

yii2:
config_path: %currentWorkingDirectory%/phpstan-yii-config.php

config_path: tests/assets/yii-config-valid.php
38 changes: 19 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true"
stopOnFailure="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
executionOrder="depends,defects"
failOnRisky="true"
failOnWarning="true"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Template">
<directory>tests</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="Yii2-PHPstan">
<directory>tests</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
13 changes: 0 additions & 13 deletions src/Example.php

This file was deleted.

76 changes: 76 additions & 0 deletions src/Reflection/ApplicationPropertiesClassReflectionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace yii\phpstan\Reflection;

use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Dummy\DummyPropertyReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use yii\base\Application as BaseApplication;
use yii\phpstan\ServiceMap;
use yii\web\Application as WebApplication;

final class ApplicationPropertiesClassReflectionExtension implements PropertiesClassReflectionExtension
{
/**
* @var AnnotationsPropertiesClassReflectionExtension
*/
private $annotationsProperties;

/**
* @var ServiceMap
*/
private $serviceMap;

/**
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;

public function __construct(
AnnotationsPropertiesClassReflectionExtension $annotationsProperties,
ReflectionProvider $reflectionProvider,
ServiceMap $serviceMap
) {
$this->annotationsProperties = $annotationsProperties;
$this->serviceMap = $serviceMap;
$this->reflectionProvider = $reflectionProvider;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if ($classReflection->getName() !== BaseApplication::class && !$classReflection->isSubclassOf(BaseApplication::class)) {
return false;
}

if ($classReflection->getName() !== WebApplication::class) {
$classReflection = $this->reflectionProvider->getClass(WebApplication::class);
}

return $classReflection->hasNativeProperty($propertyName)
|| $this->annotationsProperties->hasProperty($classReflection, $propertyName)
|| $this->serviceMap->getComponentClassById($propertyName);
}

public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
if ($classReflection->getName() !== WebApplication::class) {
$classReflection = $this->reflectionProvider->getClass(WebApplication::class);
}

if (null !== $componentClass = $this->serviceMap->getComponentClassById($propertyName)) {
return new ComponentPropertyReflection(new DummyPropertyReflection(), new ObjectType($componentClass));
}

if ($classReflection->hasNativeProperty($propertyName)) {
return $classReflection->getNativeProperty($propertyName);
}

return $this->annotationsProperties->getProperty($classReflection, $propertyName);
}
}
Loading

0 comments on commit 8e2abfe

Please sign in to comment.