Skip to content

Commit

Permalink
Merge pull request #11 from spryker-sdk/phpstan-0.12
Browse files Browse the repository at this point in the history
Upgrade to PHPStan 0.12
  • Loading branch information
dereuromark authored May 5, 2020
2 parents 19ce9b0 + 9368e18 commit 26065ce
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 33 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/vendor/*
.idea/*
/vendor/
/.idea/
/composer.phar
/composer.lock
/phpunit.phar
/.phpunit.result.cache
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
language: php

php:
- 7.1
- 7.3
- 7.2
- 7.4

env:
- dependencies=lowest
- dependencies=highest

before_script:
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi;
- if [ "$dependencies" = "highest" ]; then composer update --no-interaction; fi;

script:
- vendor/bin/phing
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Spryker PHPStan Extensions
[![Build Status](https://api.travis-ci.org/spryker-sdk/phpstan-spryker.svg?branch=master)](https://travis-ci.org/spryker-sdk/phpstan-spryker)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%207.1-8892BF.svg)](https://php.net/)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
[![License](https://poser.pugx.org/spryker/code-sniffer/license.svg)](https://packagist.org/packages/spryker-sdk/phpstan-spryker)

* [PHPStan](https://github.com/phpstan/phpstan)
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
}
],
"require": {
"php": ">=7.1",
"phpstan/phpstan": "^0.11.1"
"php": ">=7.2",
"phpstan/phpstan": "^0.12.4"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.1.1",
"jakub-onderka/php-parallel-lint": "^1.0",
"phing/phing": "^2.16.0",
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan-strict-rules": "^0.12.0",
"spryker/code-sniffer": "^0.14",
"squizlabs/php_codesniffer": "^3.3.1"
},
Expand All @@ -37,13 +37,12 @@
"test": "php phpunit.phar",
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-6.5.13.phar && mv phpunit-6.5.13.phar phpunit.phar || true",
"phpstan": "vendor/bin/phpstan analyze -c tests/phpstan.neon -l 7 src/",
"cs-check": "phpcs -v src/ tests/",
"cs-fix": "phpcbf -v src/ tests/"
"cs-check": "phpcs -p -s src/ tests/",
"cs-fix": "phpcbf -p src/ tests/"
},
"support": {
"source": "https://github.com/spryker-sdk/phpstan-spryker"
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DynamicMethodMissingPhpDocAnnotationRule implements Rule

/**
* @param string $className
* @param array $methodNames
* @param string[] $methodNames
*/
public function __construct(string $className, array $methodNames)
{
Expand Down
32 changes: 21 additions & 11 deletions src/Type/Spryker/DynamicMethodMissingTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DynamicMethodMissingTypeExtension implements DynamicMethodReturnTypeExtens
* @param \PHPStan\Reflection\Annotations\AnnotationsMethodsClassReflectionExtension $annotationsMethodsClassReflectionExtension
* @param \PHPStan\Cache\Cache $cache
* @param string $className
* @param array $methodNames
* @param string[] $methodNames
*/
public function __construct(
AnnotationsMethodsClassReflectionExtension $annotationsMethodsClassReflectionExtension,
Expand Down Expand Up @@ -89,15 +89,15 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
*/
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$cacheKey = $this->generateCacheKey($methodReflection, $scope);
$type = $this->getCachedValue($cacheKey);
[$cacheKey, $variableCacheKey] = $this->generateCacheKeys($methodReflection, $scope);
$type = $this->getCachedValue($cacheKey, $variableCacheKey);

if ($type instanceof Type) {
return $type;
}

$type = $this->getTypeFromAnnotationsMethodClassReflection($methodReflection, $scope);
$this->saveCachedValue($cacheKey, $type);
$this->saveCachedValue($cacheKey, $variableCacheKey, $type);

return $type;
}
Expand Down Expand Up @@ -127,35 +127,45 @@ protected function getTypeFromAnnotationsMethodClassReflection(MethodReflection

/**
* @param string $cacheKey
* @param string $variableCacheKey
* @param \PHPStan\Type\Type $value
*
* @return void
*/
protected function saveCachedValue(string $cacheKey, Type $value): void
protected function saveCachedValue(string $cacheKey, string $variableCacheKey, Type $value): void
{
$this->cache->save($cacheKey, $value);
$this->cache->save($cacheKey, $variableCacheKey, $value);
}

/**
* @param string $cacheKey
* @param string $variableCacheKey
*
* @return \PHPStan\Type\Type|null
*/
protected function getCachedValue(string $cacheKey): ?Type
protected function getCachedValue(string $cacheKey, string $variableCacheKey): ?Type
{
return $this->cache->load($cacheKey);
return $this->cache->load($cacheKey, $variableCacheKey);
}

/**
* @param \PHPStan\Reflection\MethodReflection $methodReflection
* @param \PHPStan\Analyser\Scope $scope
*
* @return string
* @return string[]
*/
protected function generateCacheKey(MethodReflection $methodReflection, Scope $scope): string
protected function generateCacheKeys(MethodReflection $methodReflection, Scope $scope): array
{
$filePath = $scope->getFile();
$modifiedTime = filemtime($filePath);

if ($modifiedTime === false) {
$modifiedTime = time();
}

$cacheKey = sprintf('%s-%d-%s', $filePath, filemtime($filePath), $methodReflection->getName());
$variableCacheKey = sprintf('%d-filemtime', $modifiedTime);

return sprintf('%s-%d-%s', $filePath, filemtime($filePath), $methodReflection->getName());
return [$cacheKey, $variableCacheKey];
}
}
29 changes: 19 additions & 10 deletions tests/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
includes:
- ../vendor/phpstan/phpstan-strict-rules/rules.neon
- ../vendor/phpstan/phpstan-strict-rules/rules.neon

services:
scopeIsInClass:
class: PHPStan\Internal\ScopeIsInClassTypeSpecifyingExtension
arguments:
isInMethodName: isInClass
removeNullMethodName: getClassReflection
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
scopeIsInClass:
class: PHPStan\Internal\ScopeIsInClassTypeSpecifyingExtension
arguments:
isInMethodName: isInClass
removeNullMethodName: getClassReflection
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension

parameters:
ignoreErrors:
- '#Parameter \#1 .+ should be contravariant with parameter .+ of method .+Rule::processNode\(\)#'
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
-
message: "#^Parameter \\#1 \\$node \\(PhpParser\\\\Node\\\\Expr\\\\MethodCall\\) of method PHPStan\\\\Rules\\\\Spryker\\\\DynamicMethodMissingPhpDocAnnotationRule\\:\\:processNode\\(\\) should be contravariant with parameter \\$node \\(PhpParser\\\\Node\\) of method PHPStan\\\\Rules\\\\Rule\\<PhpParser\\\\Node\\>\\:\\:processNode\\(\\)$#"
count: 1
path: ../src/Rules/Spryker/DynamicMethodMissingPhpDocAnnotationRule.php
-
message: "#.+DynamicMethodMissingPhpDocAnnotationRule::getNodeType\\(\\) should be covariant with return type .+ of method .+Node\\>::getNodeType\\(\\)#"
count: 1
path: ../src/Rules/Spryker/DynamicMethodMissingPhpDocAnnotationRule.php

0 comments on commit 26065ce

Please sign in to comment.