phpstan lvl 7

This commit is contained in:
TomasVotruba 2017-11-06 14:54:16 +01:00
parent f779062784
commit a2097fec4f
17 changed files with 52 additions and 18 deletions

View File

@ -57,7 +57,7 @@
"all": ["@check-cs", "phpunit"],
"check-cs": "ecs check bin packages src tests",
"fix-cs": "ecs check bin packages src tests --fix",
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level 6 --configuration phpstan.neon"
"phpstan": "vendor/bin/phpstan.phar analyse packages src tests --level 7 --configuration phpstan.neon"
},
"bin": [
"bin/rector",

View File

@ -64,3 +64,6 @@ parameters:
- bin/rector.php
# test file
- tests/Rector/Contrib/Nette/Routing/BootstrapToRouterFactoryRector/Wrong/bootstrap.php
PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff:
# intentionally
- packages/BetterReflection/src/Reflector/SmartClassReflector.php

View File

@ -54,6 +54,7 @@ final class SmartClassReflector
}
/**
* @todo validate at least one is passed, or split to 2 methods?
* @return string[]
*/
public function getClassParents(?string $className = null, ?ClassLike $classLikeNode = null): array
@ -67,17 +68,15 @@ final class SmartClassReflector
return [];
}
$classReflection = $this->reflect($className);
try {
$classReflection = $this->reflect($className);
return $classReflection->getParentClassNames();
} catch (Throwable $throwable) {
if ($classLikeNode) {
return $this->resolveClassParentsFromNode($classLikeNode);
}
// intentionally empty
}
return [];
return $this->resolveClassParentsFromNode($classLikeNode);
}
private function createNewClassReflector(): void

View File

@ -73,7 +73,7 @@ final class DeprecationDetector extends NodeVisitorAbstract
private function processDocBlockDeprecation(Node $node): void
{
$deprecation = $this->docBlockAnalyzer->getDeprecatedDocComment($node);
if ($deprecation === '') {
if (! is_string($deprecation)) {
return;
}

View File

@ -25,6 +25,7 @@ final class BetterNodeFinderTest extends AbstractNodeTypeResolverTest
public function testFindFirstAncestorInstanceOf(): void
{
/** @var Variable $variableNode */
$variableNode = $this->betterNodeFinder->findFirstInstanceOf($this->nodes, Variable::class);
$classNode = $this->betterNodeFinder->findFirstInstanceOf($this->nodes, Class_::class);
@ -34,6 +35,7 @@ final class BetterNodeFinderTest extends AbstractNodeTypeResolverTest
public function testFindMissingFirstAncestorInstanceOf(): void
{
/** @var Variable $variableNode */
$variableNode = $this->betterNodeFinder->findFirstInstanceOf($this->nodes, Variable::class);
$this->assertNull($this->betterNodeFinder->findFirstAncestorInstanceOf($variableNode, Array_::class));

View File

@ -62,7 +62,7 @@ final class DocBlockAnalyzer
{
/** @var Var_[] $varTags */
$varTags = $this->getTagsByName($node, 'var');
if ($varTags === null) {
if (! count($varTags)) {
return null;
}
@ -78,7 +78,7 @@ final class DocBlockAnalyzer
{
/** @var Deprecated[] $deprecatedTags */
$deprecatedTags = $this->getTagsByName($node, 'deprecated');
if ($deprecatedTags === null) {
if (! count($deprecatedTags)) {
return null;
}
@ -96,7 +96,7 @@ final class DocBlockAnalyzer
/** @var Param[] $paramTags */
$paramTags = $this->getTagsByName($node, 'param');
if ($paramTags === null) {
if (! count($paramTags)) {
return null;
}

View File

@ -16,7 +16,7 @@ final class NamespaceAnalyzer
{
/** @var Use_[] $useNodes */
$useNodes = $node->getAttribute(Attribute::USE_NODES);
if ($useNodes === null) {
if (! count($useNodes)) {
return false;
}

View File

@ -23,6 +23,12 @@ parameters:
- '#Array \(PhpParser\\Node\\Stmt\[\]\) does not accept PhpParser\\Node#'
- '#Property Rector\\NodeTypeResolver\\NodeVisitor\\NamespaceResolver::\$useNodes \(PhpParser\\Node\\Stmt\\Use_\[\]\) does not accept PhpParser\\Node\[\]#'
- '#Method Rector\\NodeValueResolver\\PerNodeValueResolver\\ArgValueResolver::resolve\(\) should return string\|null but returns bool\|string\|null#'
- '#Calling method (getRealPath|getFilename)\(\) on possibly null value of type SplFileInfo\|null#'
- '#Calling method toString\(\) on possibly null value of type PhpParser\\Node\\Name\\FullyQualified\|null#'
- '#Calling method toString\(\) on possibly null value of type PhpParser\\Node\\Name\|null#'
- '#Calling method getText\(\) on possibly null value of type PhpParser\\Comment\\Doc\|null#'
- '#Parameter \#1 \$classLikeNode of method Rector\\BetterReflection\\Reflector\\SmartClassReflector::resolveClassParentsFromNode\(\) expects PhpParser\\Node\\Stmt\\ClassLike, PhpParser\\Node\\Stmt\\ClassLike\|null given#'
- '#Calling method getParentClassNames\(\) on possibly null value of type Rector\\BetterReflection\\Reflection\\ReflectionClass\|null#'
# buggy
- '#Property Rector\\Rector\\Contrib\\PHPUnit\\SpecificMethodRector::\$oldToNewMethods \(false\[\]\[\]|string\[\]\[\]\) does not accept default value of type \(false|string\)\[\]\[\]#'

View File

@ -19,7 +19,10 @@ final class AppKernel extends Kernel
public function __construct(?string $configFile = '')
{
$this->configFile = $configFile;
if ($configFile) {
$this->configFile = $configFile;
}
parent::__construct('dev' . sha1($configFile), true);
}

View File

@ -84,7 +84,7 @@ final class ClassLikeAnalyzer
return $node->toString();
}
return $node->name->toString();
return $node->name ? $node->name->toString() : '';
}
/**

View File

@ -157,6 +157,10 @@ final class MethodCallAnalyzer
}
$classReflection = $this->smartClassReflector->reflect($type);
if ($classReflection === null) {
return [];
}
$publicMethods = $classReflection->getMethods(ReflectionMethod::IS_PUBLIC);
return $this->publicMethodNamesForType[$type] = array_keys($publicMethods);

View File

@ -142,6 +142,10 @@ final class PropertyFetchAnalyzer
}
$classReflection = $this->smartClassReflector->reflect($type);
if ($classReflection === null) {
return [];
}
$publicProperties = $classReflection->getProperties(ReflectionProperty::IS_PUBLIC);
return $this->publicPropertyNamesForType[$type] = array_keys($publicProperties);

View File

@ -3,6 +3,7 @@
namespace Rector\Parser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Parser as NikicParser;
use Rector\Contract\Parser\ParserInterface;
@ -14,7 +15,7 @@ final class Parser implements ParserInterface
private $nikicParser;
/**
* @var Node[][]
* @var Stmt[][]|null[]
*/
private $nodesByFile = [];

View File

@ -5,6 +5,7 @@ namespace Rector\Rector\Contrib\Nette\Bootstrap;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
@ -40,7 +41,7 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
{
/** @var Node\Name\FullyQualified $fqnName */
/** @var FullyQualified|null $fqnName */
$fqnName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
if ($fqnName === null && $classConstFetchNode->class instanceof Variable) {

View File

@ -81,8 +81,14 @@ final class MagicMethodRector extends AbstractRector
/** @var string $className */
$className = $node->getAttribute(Attribute::CLASS_NAME);
$classReflection = $this->smartClassReflector->reflect($className);
if ($classReflection === null) {
return false;
}
$this->magicMethods = $this->magicMethodMatcher->matchInContent(
$this->smartClassReflector->reflect($className),
$classReflection,
$docComments[0]->getText()
);

View File

@ -49,6 +49,9 @@ final class ClassReplacerRector extends AbstractRector
}
$nameNode = $this->resolveNameNodeFromNode($node);
if ($nameNode === null) {
return false;
}
return isset($this->oldToNewClasses[$nameNode->toString()]);
}

View File

@ -87,7 +87,7 @@ final class NamespaceReplacerRector extends AbstractRector
private function resolveNameFromNode(Node $node): string
{
if ($node instanceof Namespace_) {
if ($node instanceof Namespace_ && $node->name) {
return $node->name->toString();
}
@ -104,6 +104,8 @@ final class NamespaceReplacerRector extends AbstractRector
return $node->toString();
}
return '';
}
private function isNamespaceToChange(string $namespace): bool