Skip to content

Instantly share code, notes, and snippets.

@AJenbo
Created August 19, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AJenbo/595c9afcfc201d4ab7262e2464d98e5d to your computer and use it in GitHub Desktop.
Save AJenbo/595c9afcfc201d4ab7262e2464d98e5d to your computer and use it in GitHub Desktop.
pdepend PHPStan intergration
{
"name": "pdepend/pdepend",
"description": "Official version of pdepend to be handled with Composer",
"license": "BSD-3-Clause",
"type": "library",
"require": {
"php": ">=5.3.7",
"symfony/dependency-injection": "^2.3.0|^3|^4",
"symfony/filesystem": "^2.3.0|^3|^4",
"symfony/config": "^2.3.0|^3|^4"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.7",
"squizlabs/php_codesniffer": "^2.0.0",
"gregwar/rst": "^1.0",
"easy-doc/easy-doc": "0.0.0 || ^1.2.3",
"phpstan/phpstan": "^0.11.15"
},
"bin": ["src/bin/pdepend"],
"autoload": {
"psr-4": {"PDepend\\": "src/main/php/PDepend"}
},
"autoload-dev": {
"psr-4": {"PDepend\\PHPStan\\": "src/PHPStan"}
},
"scripts": {
"test": "phpunit",
"cs-check": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 ./src/main/php ./src/test/php",
"cs-fix": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 ./src/main/php ./src/test/php",
"build-website": "easy-doc build src/site/config.php --verbose"
}
}
<?php declare(strict_types = 1);
namespace PDepend\PHPStan\Type;
use PDepend\Source\Language\PHP\PHPBuilder;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
class PHPBuilderDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{
/** @var int[] */
private $methods = [
'buildAstNodeInstance' => 0,
];
public function getClass(): string
{
return PHPBuilder::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return array_key_exists($methodReflection->getName(), $this->methods);
}
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$argumentIndex = $this->methods[$methodReflection->getName()];
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
if (!isset($methodCall->args[$argumentIndex])) {
return $parametersAcceptor->getReturnType();
}
$argType = $scope->getType($methodCall->args[$argumentIndex]->value);
if (!$argType instanceof ConstantStringType) {
return $parametersAcceptor->getReturnType();
}
$class = $argType->getValue();
return TypeCombinator::intersect(
new ObjectType("\\PDepend\\Source\\AST\\{$class}"),
$parametersAcceptor->getReturnType()
);
}
}
includes:
- vendor/phpstan/phpstan/conf/config.level2.neon
parameters:
paths:
- src/main
excludes_analyse:
- src/main/php/PDepend/DbusUI/ResultPrinter.php
- src/main/php/PDepend/Util/ImageConvert.php
universalObjectCratesClasses:
- PDepend\Util\Configuration
ignoreErrors:
- '#Call to an undefined method#'
- '#does not call parent constructor from#'
services:
-
class: PDepend\PHPStan\Type\PHPBuilderDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment