From 629ccf6b02146e1cbf0499982358c595e3d6edd5 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 23 Sep 2021 10:07:14 +0200 Subject: [PATCH] [BCB] Changed return types of various methods that had |false to |null --- composer.lock | 8 +-- src/Analyser/FileAnalyser.php | 2 +- src/Analyser/MutatingScope.php | 8 +-- src/Analyser/NodeScopeResolver.php | 2 +- .../ResultCache/ResultCacheManager.php | 2 +- src/Analyser/TypeSpecifier.php | 2 +- src/Dependency/DependencyResolver.php | 2 +- src/Dependency/NodeDependencies.php | 2 +- src/PhpDoc/PhpDocBlock.php | 2 +- src/PhpDoc/TypeNodeResolver.php | 4 +- .../BetterReflectionProvider.php | 7 ++- src/Reflection/ClassConstantReflection.php | 7 +-- src/Reflection/ClassReflection.php | 54 ++++++++----------- src/Reflection/FunctionReflectionFactory.php | 4 +- .../Php/BuiltinMethodReflection.php | 15 ++---- .../Php/FakeBuiltinMethodReflection.php | 21 +++----- .../Php/NativeBuiltinMethodReflection.php | 36 +++++++------ .../Php/PhpClassReflectionExtension.php | 12 ++--- src/Reflection/Php/PhpFunctionReflection.php | 18 +++---- src/Reflection/Php/PhpMethodReflection.php | 2 +- src/Reflection/ReflectionWithFilename.php | 5 +- .../Runtime/RuntimeReflectionProvider.php | 2 +- src/Rules/Classes/ClassConstantRule.php | 2 +- src/Rules/Classes/InstantiationRule.php | 2 +- .../Constants/OverridingConstantRule.php | 2 +- .../Generics/CrossCheckInterfacesHelper.php | 4 +- src/Rules/Methods/CallMethodsRule.php | 2 +- src/Rules/Methods/CallStaticMethodsRule.php | 2 +- src/Rules/Methods/MethodSignatureRule.php | 2 +- src/Rules/Methods/OverridingMethodRule.php | 2 +- src/Rules/Properties/AccessPropertiesRule.php | 2 +- .../Properties/AccessStaticPropertiesRule.php | 2 +- .../Properties/OverridingPropertyRule.php | 2 +- src/Type/FileTypeMapper.php | 2 +- src/Type/ObjectType.php | 4 +- src/Type/ParserNodeTypeToPHPStanType.php | 2 +- ...lassDynamicFunctionReturnTypeExtension.php | 2 +- src/Type/TypehintHelper.php | 2 +- .../Reflection/ClassReflectionTest.php | 2 +- 39 files changed, 110 insertions(+), 145 deletions(-) diff --git a/composer.lock b/composer.lock index b908faaf7c..2c36f66299 100644 --- a/composer.lock +++ b/composer.lock @@ -4474,12 +4474,12 @@ "source": { "type": "git", "url": "https://github.com/phpstan/phpstan-strict-rules.git", - "reference": "6c7a9815bb3af49113a6f85d073e3e2fc01f7ffa" + "reference": "a9b2cb1ee2668ac3a66b5f76f37122e64299c2ca" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/6c7a9815bb3af49113a6f85d073e3e2fc01f7ffa", - "reference": "6c7a9815bb3af49113a6f85d073e3e2fc01f7ffa", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/a9b2cb1ee2668ac3a66b5f76f37122e64299c2ca", + "reference": "a9b2cb1ee2668ac3a66b5f76f37122e64299c2ca", "shasum": "" }, "require": { @@ -4518,7 +4518,7 @@ "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", "source": "https://github.com/phpstan/phpstan-strict-rules/tree/master" }, - "time": "2021-09-22T16:41:47+00:00" + "time": "2021-09-23T08:29:12+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Analyser/FileAnalyser.php b/src/Analyser/FileAnalyser.php index af21ba3f71..c13f5429b3 100644 --- a/src/Analyser/FileAnalyser.php +++ b/src/Analyser/FileAnalyser.php @@ -108,7 +108,7 @@ public function analyseFile( $metadata = []; if ($scope->isInTrait()) { $traitReflection = $scope->getTraitReflection(); - if ($traitReflection->getFileName() !== false) { + if ($traitReflection->getFileName() !== null) { $traitFilePath = $traitReflection->getFileName(); } } diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index be1d52b3f9..395b8126dd 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -292,7 +292,7 @@ public function getFileDescription(): string } $traitReflection = $this->context->getTraitReflection(); - if ($traitReflection->getFileName() === false) { + if ($traitReflection->getFileName() === null) { throw new \PHPStan\ShouldNotHappenException(); } @@ -2588,7 +2588,7 @@ private function resolveExactName(Name $name): ?string return null; } $currentClassReflection = $this->getClassReflection(); - if ($currentClassReflection->getParentClass() !== false) { + if ($currentClassReflection->getParentClass() !== null) { return $currentClassReflection->getParentClass()->getName(); } return null; @@ -2614,7 +2614,7 @@ public function resolveName(Name $name): string return $this->getClassReflection()->getName(); } elseif ($originalClass === 'parent') { $currentClassReflection = $this->getClassReflection(); - if ($currentClassReflection->getParentClass() !== false) { + if ($currentClassReflection->getParentClass() !== null) { return $currentClassReflection->getParentClass()->getName(); } } @@ -3385,7 +3385,7 @@ public function getFunctionType($type, bool $isNullable, bool $isVariadic): Type $className = (string) $type; $lowercasedClassName = strtolower($className); if ($lowercasedClassName === 'parent') { - if ($this->isInClass() && $this->getClassReflection()->getParentClass() !== false) { + if ($this->isInClass() && $this->getClassReflection()->getParentClass() !== null) { return new ObjectType($this->getClassReflection()->getParentClass()->getName()); } diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 5e3e16c307..cbde3f80ee 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -3663,7 +3663,7 @@ private function processTraitUse(Node\Stmt\TraitUse $node, MutatingScope $classS } $traitReflection = $this->reflectionProvider->getClass($traitName); $traitFileName = $traitReflection->getFileName(); - if ($traitFileName === false) { + if ($traitFileName === null) { continue; // trait from eval or from PHP itself } $fileName = $this->fileHelper->normalizePath($traitFileName); diff --git a/src/Analyser/ResultCache/ResultCacheManager.php b/src/Analyser/ResultCache/ResultCacheManager.php index 71ae46d78c..46b2205628 100644 --- a/src/Analyser/ResultCache/ResultCacheManager.php +++ b/src/Analyser/ResultCache/ResultCacheManager.php @@ -633,7 +633,7 @@ private function getProjectExtensionFiles(?array $projectConfig, array $dependen $classReflection = $this->reflectionProvider->getClass($class); $fileName = $classReflection->getFileName(); - if ($fileName === false) { + if ($fileName === null) { continue; } diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index fbf31d29f4..5a6453374b 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -123,7 +123,7 @@ public function specifyTypesInCondition( } elseif ($lowercasedClassName === 'parent') { if ( $scope->isInClass() - && $scope->getClassReflection()->getParentClass() !== false + && $scope->getClassReflection()->getParentClass() !== null ) { $type = new ObjectType($scope->getClassReflection()->getParentClass()->getName()); } else { diff --git a/src/Dependency/DependencyResolver.php b/src/Dependency/DependencyResolver.php index 3580e529cd..5507705a72 100644 --- a/src/Dependency/DependencyResolver.php +++ b/src/Dependency/DependencyResolver.php @@ -230,7 +230,7 @@ private function addClassToDependencies(string $className, array &$dependenciesR } $classReflection = $classReflection->getParentClass(); - } while ($classReflection !== false); + } while ($classReflection !== null); } private function getFunctionReflection(\PhpParser\Node\Name $nameNode, ?Scope $scope): ReflectionWithFilename diff --git a/src/Dependency/NodeDependencies.php b/src/Dependency/NodeDependencies.php index cd75370e7d..b6fcb0ce51 100644 --- a/src/Dependency/NodeDependencies.php +++ b/src/Dependency/NodeDependencies.php @@ -41,7 +41,7 @@ public function getFileDependencies(string $currentFile, array $analysedFiles): foreach ($this->reflections as $dependencyReflection) { $dependencyFile = $dependencyReflection->getFileName(); - if ($dependencyFile === false) { + if ($dependencyFile === null) { continue; } $dependencyFile = $this->fileHelper->normalizePath($dependencyFile); diff --git a/src/PhpDoc/PhpDocBlock.php b/src/PhpDoc/PhpDocBlock.php index 92fa123c71..3127628d9f 100644 --- a/src/PhpDoc/PhpDocBlock.php +++ b/src/PhpDoc/PhpDocBlock.php @@ -341,7 +341,7 @@ private static function getParentReflections(ClassReflection $classReflection): $result = []; $parent = $classReflection->getParentClass(); - if ($parent !== false) { + if ($parent !== null) { $result[] = $parent; } diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 10c701a90a..39ac549568 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -310,7 +310,7 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco case 'parent': if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); - if ($classReflection->getParentClass() !== false) { + if ($classReflection->getParentClass() !== null) { return new ObjectType($classReflection->getParentClass()->getName()); } } @@ -681,7 +681,7 @@ private function resolveConstTypeNode(ConstTypeNode $typeNode, NameScope $nameSc case 'parent': if ($this->getReflectionProvider()->hasClass($nameScope->getClassName())) { $classReflection = $this->getReflectionProvider()->getClass($nameScope->getClassName()); - if ($classReflection->getParentClass() === false) { + if ($classReflection->getParentClass() === null) { return new ErrorType(); } diff --git a/src/Reflection/BetterReflection/BetterReflectionProvider.php b/src/Reflection/BetterReflection/BetterReflectionProvider.php index 850eb36364..f26041e01a 100644 --- a/src/Reflection/BetterReflection/BetterReflectionProvider.php +++ b/src/Reflection/BetterReflection/BetterReflectionProvider.php @@ -210,7 +210,7 @@ public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNo $scopeFile = $scope->getFile(); } else { $scopeFile = $scope->getTraitReflection()->getFileName(); - if ($scopeFile === false) { + if ($scopeFile === null) { $scopeFile = $scope->getFile(); } } @@ -297,9 +297,8 @@ private function getCustomFunction(string $functionName): \PHPStan\Reflection\Ph return $parameter->getName(); }, $reflectionFunction->getParameters())); if ($resolvedPhpDoc === null && $reflectionFunction->getFileName() !== false && $reflectionFunction->getDocComment() !== false) { - $fileName = $reflectionFunction->getFileName(); $docComment = $reflectionFunction->getDocComment(); - $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($fileName, null, null, $reflectionFunction->getName(), $docComment); + $resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc($reflectionFunction->getFileName(), null, null, $reflectionFunction->getName(), $docComment); } if ($resolvedPhpDoc !== null) { @@ -326,7 +325,7 @@ private function getCustomFunction(string $functionName): \PHPStan\Reflection\Ph $isDeprecated, $isInternal, $isFinal, - $reflectionFunction->getFileName(), + $reflectionFunction->getFileName() !== false ? $reflectionFunction->getFileName() : null, $isPure ); } diff --git a/src/Reflection/ClassConstantReflection.php b/src/Reflection/ClassConstantReflection.php index 88f87bf8f7..a89d59d7cc 100644 --- a/src/Reflection/ClassConstantReflection.php +++ b/src/Reflection/ClassConstantReflection.php @@ -53,12 +53,7 @@ public function getName(): string public function getFileName(): ?string { - $fileName = $this->declaringClass->getFileName(); - if ($fileName === false) { - return null; - } - - return $fileName; + return $this->declaringClass->getFileName(); } /** diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index eee291e8a7..80b1cb4c09 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -97,16 +97,16 @@ class ClassReflection implements ReflectionWithFilename private array $subclasses = []; /** @var string|false|null */ - private $filename; + private $filename = false; /** @var string|false|null */ - private $reflectionDocComment; + private $reflectionDocComment = false; /** @var \PHPStan\Reflection\ClassReflection[]|null */ private ?array $cachedInterfaces = null; /** @var \PHPStan\Reflection\ClassReflection|false|null */ - private $cachedParentClass = null; + private $cachedParentClass = false; /** @var array|null */ private ?array $typeAliases = null; @@ -161,12 +161,9 @@ public function getNativeReflection(): \ReflectionClass return $this->reflection; } - /** - * @return string|false - */ - public function getFileName() + public function getFileName(): ?string { - if (isset($this->filename)) { + if ($this->filename !== false) { return $this->filename; } @@ -175,11 +172,11 @@ public function getFileName() } $fileName = $this->reflection->getFileName(); if ($fileName === false) { - return $this->filename = false; + return $this->filename = null; } if (!file_exists($fileName)) { - return $this->filename = false; + return $this->filename = null; } return $this->filename = $fileName; @@ -191,27 +188,19 @@ public function getFileNameWithPhpDocs(): ?string return $this->stubPhpDocBlock->getFilename(); } - $filename = $this->getFileName(); - if ($filename === false) { - return null; - } - - return $filename; + return $this->getFileName(); } - /** - * @return false|\PHPStan\Reflection\ClassReflection - */ - public function getParentClass() + public function getParentClass(): ?ClassReflection { - if ($this->cachedParentClass !== null) { + if ($this->cachedParentClass !== false) { return $this->cachedParentClass; } $parentClass = $this->reflection->getParentClass(); if ($parentClass === false) { - return $this->cachedParentClass = false; + return $this->cachedParentClass = null; } $extendsTag = $this->getFirstExtendsTag(); @@ -575,7 +564,7 @@ public function getParents(): array { $parents = []; $parent = $this->getParentClass(); - while ($parent !== false) { + while ($parent !== null) { $parents[] = $parent; $parent = $parent->getParentClass(); } @@ -595,7 +584,7 @@ public function getInterfaces(): array $interfaces = $this->getImmediateInterfaces(); $immediateInterfaces = $interfaces; $parent = $this->getParentClass(); - while ($parent !== false) { + while ($parent !== null) { foreach ($parent->getImmediateInterfaces() as $parentInterface) { $interfaces[$parentInterface->getName()] = $parentInterface; foreach ($this->collectInterfaces($parentInterface) as $parentInterfaceInterface) { @@ -640,7 +629,7 @@ public function getImmediateInterfaces(): array { $indirectInterfaceNames = []; $parent = $this->getParentClass(); - while ($parent !== false) { + while ($parent !== null) { foreach ($parent->getNativeReflection()->getInterfaceNames() as $parentInterfaceName) { $indirectInterfaceNames[] = $parentInterfaceName; } @@ -740,7 +729,7 @@ public function getParentClassesNames(): array { $parentNames = []; $currentClassReflection = $this; - while ($currentClassReflection->getParentClass() !== false) { + while ($currentClassReflection->getParentClass() !== null) { $parentNames[] = $currentClassReflection->getParentClass()->getName(); $currentClassReflection = $currentClassReflection->getParentClass(); } @@ -780,7 +769,7 @@ public function getConstant(string $name): ConstantReflection $declaringClass->getName(), $name ); - if ($resolvedPhpDoc === null && $fileName !== false) { + if ($resolvedPhpDoc === null && $fileName !== null) { $docComment = null; if ($reflectionConstant->getDocComment() !== false) { $docComment = $reflectionConstant->getDocComment(); @@ -1097,15 +1086,16 @@ public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock } $fileName = $this->getFileName(); - if ($fileName === false) { + if ($fileName === null) { return null; } - if ($this->reflectionDocComment === null) { - $this->reflectionDocComment = $this->reflection->getDocComment(); + if ($this->reflectionDocComment === false) { + $docComment = $this->reflection->getDocComment(); + $this->reflectionDocComment = $docComment !== false ? $docComment : null; } - if ($this->reflectionDocComment === false) { + if ($this->reflectionDocComment === null) { return null; } @@ -1189,7 +1179,7 @@ public function getAncestors(): array } $parent = $this->getParentClass(); - if ($parent !== false) { + if ($parent !== null) { $addToAncestors($parent->getName(), $parent); foreach ($parent->getAncestors() as $name => $ancestor) { $addToAncestors($name, $ancestor); diff --git a/src/Reflection/FunctionReflectionFactory.php b/src/Reflection/FunctionReflectionFactory.php index caf4d05e72..dccd3a19b0 100644 --- a/src/Reflection/FunctionReflectionFactory.php +++ b/src/Reflection/FunctionReflectionFactory.php @@ -19,7 +19,7 @@ interface FunctionReflectionFactory * @param bool $isDeprecated * @param bool $isInternal * @param bool $isFinal - * @param string|false $filename + * @param string|null $filename * @param bool|null $isPure * @return PhpFunctionReflection */ @@ -33,7 +33,7 @@ public function create( bool $isDeprecated, bool $isInternal, bool $isFinal, - $filename, + ?string $filename, ?bool $isPure = null ): PhpFunctionReflection; diff --git a/src/Reflection/Php/BuiltinMethodReflection.php b/src/Reflection/Php/BuiltinMethodReflection.php index efc2f4e0b4..266d002075 100644 --- a/src/Reflection/Php/BuiltinMethodReflection.php +++ b/src/Reflection/Php/BuiltinMethodReflection.php @@ -11,22 +11,13 @@ public function getName(): string; public function getReflection(): ?\ReflectionMethod; - /** - * @return string|false - */ - public function getFileName(); + public function getFileName(): ?string; public function getDeclaringClass(): \ReflectionClass; - /** - * @return int|false - */ - public function getStartLine(); + public function getStartLine(): ?int; - /** - * @return int|false - */ - public function getEndLine(); + public function getEndLine(): ?int; public function getDocComment(): ?string; diff --git a/src/Reflection/Php/FakeBuiltinMethodReflection.php b/src/Reflection/Php/FakeBuiltinMethodReflection.php index a1737af26c..cfb2d1b831 100644 --- a/src/Reflection/Php/FakeBuiltinMethodReflection.php +++ b/src/Reflection/Php/FakeBuiltinMethodReflection.php @@ -30,12 +30,9 @@ public function getReflection(): ?\ReflectionMethod return null; } - /** - * @return string|false - */ - public function getFileName() + public function getFileName(): ?string { - return false; + return null; } public function getDeclaringClass(): \ReflectionClass @@ -43,20 +40,14 @@ public function getDeclaringClass(): \ReflectionClass return $this->declaringClass; } - /** - * @return int|false - */ - public function getStartLine() + public function getStartLine(): ?int { - return false; + return null; } - /** - * @return int|false - */ - public function getEndLine() + public function getEndLine(): ?int { - return false; + return null; } public function getDocComment(): ?string diff --git a/src/Reflection/Php/NativeBuiltinMethodReflection.php b/src/Reflection/Php/NativeBuiltinMethodReflection.php index 8a59bb0237..692c06a010 100644 --- a/src/Reflection/Php/NativeBuiltinMethodReflection.php +++ b/src/Reflection/Php/NativeBuiltinMethodReflection.php @@ -24,12 +24,14 @@ public function getReflection(): ?\ReflectionMethod return $this->reflection; } - /** - * @return string|false - */ - public function getFileName() + public function getFileName(): ?string { - return $this->reflection->getFileName(); + $fileName = $this->reflection->getFileName(); + if ($fileName === false) { + return null; + } + + return $fileName; } public function getDeclaringClass(): \ReflectionClass @@ -37,20 +39,24 @@ public function getDeclaringClass(): \ReflectionClass return $this->reflection->getDeclaringClass(); } - /** - * @return int|false - */ - public function getStartLine() + public function getStartLine(): ?int { - return $this->reflection->getStartLine(); + $line = $this->reflection->getStartLine(); + if ($line === false) { + return null; + } + + return $line; } - /** - * @return int|false - */ - public function getEndLine() + public function getEndLine(): ?int { - return $this->reflection->getEndLine(); + $line = $this->reflection->getEndLine(); + if ($line === false) { + return null; + } + + return $line; } public function getDocComment(): ?string diff --git a/src/Reflection/Php/PhpClassReflectionExtension.php b/src/Reflection/Php/PhpClassReflectionExtension.php index 223476e4c6..0692f245c4 100644 --- a/src/Reflection/Php/PhpClassReflectionExtension.php +++ b/src/Reflection/Php/PhpClassReflectionExtension.php @@ -219,7 +219,7 @@ private function createProperty( ); $stubPhpDocString = null; if ($resolvedPhpDoc === null) { - if ($declaringClassReflection->getFileName() !== false) { + if ($declaringClassReflection->getFileName() !== null) { $declaringTraitName = $this->findPropertyTrait($propertyReflection); $constructorName = null; if (method_exists($propertyReflection, 'isPromoted') && $propertyReflection->isPromoted()) { @@ -262,7 +262,7 @@ private function createProperty( } if ($phpDocType === null) { - if (isset($constructorName) && $declaringClassReflection->getFileName() !== false) { + if (isset($constructorName) && $declaringClassReflection->getFileName() !== null) { $constructorDocComment = $declaringClassReflection->getConstructor()->getDocComment(); $nativeClassReflection = $declaringClassReflection->getNativeReflection(); $positionalParameterNames = []; @@ -302,7 +302,7 @@ private function createProperty( if ( $phpDocType === null && $this->inferPrivatePropertyTypeFromConstructor - && $declaringClassReflection->getFileName() !== false + && $declaringClassReflection->getFileName() !== null && $propertyReflection->isPrivate() && (!method_exists($propertyReflection, 'hasType') || !$propertyReflection->hasType()) && $declaringClassReflection->hasConstructor() @@ -578,7 +578,7 @@ private function createMethod( $stubPhpDocString = null; if ($resolvedPhpDoc === null) { - if ($declaringClass->getFileName() !== false) { + if ($declaringClass->getFileName() !== null) { $docComment = $methodReflection->getDocComment(); $positionalParameterNames = array_map(static function (\ReflectionParameter $parameter): string { return $parameter->getName(); @@ -618,7 +618,7 @@ private function createMethod( if ( $methodReflection instanceof NativeBuiltinMethodReflection && $methodReflection->isConstructor() - && $declaringClass->getFileName() !== false + && $declaringClass->getFileName() !== null ) { foreach ($methodReflection->getParameters() as $parameter) { if (!method_exists($parameter, 'isPromoted') || !$parameter->isPromoted()) { @@ -923,7 +923,7 @@ private function inferAndCachePropertyTypes( if (isset($this->propertyTypesCache[$declaringClass->getName()])) { return $this->propertyTypesCache[$declaringClass->getName()]; } - if ($declaringClass->getFileName() === false) { + if ($declaringClass->getFileName() === null) { return $this->propertyTypesCache[$declaringClass->getName()] = []; } diff --git a/src/Reflection/Php/PhpFunctionReflection.php b/src/Reflection/Php/PhpFunctionReflection.php index f16327cfd5..3193522627 100644 --- a/src/Reflection/Php/PhpFunctionReflection.php +++ b/src/Reflection/Php/PhpFunctionReflection.php @@ -49,8 +49,7 @@ class PhpFunctionReflection implements FunctionReflection, ReflectionWithFilenam private bool $isFinal; - /** @var string|false */ - private $filename; + private ?string $filename; private ?bool $isPure; @@ -70,7 +69,7 @@ class PhpFunctionReflection implements FunctionReflection, ReflectionWithFilenam * @param bool $isDeprecated * @param bool $isInternal * @param bool $isFinal - * @param string|false $filename + * @param string|null $filename * @param bool|null $isPure */ public function __construct( @@ -86,7 +85,7 @@ public function __construct( bool $isDeprecated, bool $isInternal, bool $isFinal, - $filename, + ?string $filename, ?bool $isPure = null ) { @@ -111,17 +110,14 @@ public function getName(): string return $this->reflection->getName(); } - /** - * @return string|false - */ - public function getFileName() + public function getFileName(): ?string { - if ($this->filename === false) { - return false; + if ($this->filename === null) { + return null; } if (!file_exists($this->filename)) { - return false; + return null; } return $this->filename; diff --git a/src/Reflection/Php/PhpMethodReflection.php b/src/Reflection/Php/PhpMethodReflection.php index c6fab4535e..50cbd8d81a 100644 --- a/src/Reflection/Php/PhpMethodReflection.php +++ b/src/Reflection/Php/PhpMethodReflection.php @@ -268,7 +268,7 @@ private function isVariadic(): bool $filename = $this->declaringTrait->getFileName(); } - if (!$isNativelyVariadic && $filename !== false && file_exists($filename)) { + if (!$isNativelyVariadic && $filename !== null && file_exists($filename)) { $modifiedTime = filemtime($filename); if ($modifiedTime === false) { $modifiedTime = time(); diff --git a/src/Reflection/ReflectionWithFilename.php b/src/Reflection/ReflectionWithFilename.php index 5bedac9600..c626dbb7cf 100644 --- a/src/Reflection/ReflectionWithFilename.php +++ b/src/Reflection/ReflectionWithFilename.php @@ -6,9 +6,6 @@ interface ReflectionWithFilename { - /** - * @return string|false - */ - public function getFileName(); + public function getFileName(): ?string; } diff --git a/src/Reflection/Runtime/RuntimeReflectionProvider.php b/src/Reflection/Runtime/RuntimeReflectionProvider.php index 9e5a5bc98a..daf6fa24f9 100644 --- a/src/Reflection/Runtime/RuntimeReflectionProvider.php +++ b/src/Reflection/Runtime/RuntimeReflectionProvider.php @@ -308,7 +308,7 @@ private function getCustomFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope $isDeprecated, $isInternal, $isFinal, - $reflectionFunction->getFileName(), + $reflectionFunction->getFileName() !== false ? $reflectionFunction->getFileName() : null, $isPure ); $this->customFunctionReflections[$lowerCasedFunctionName] = $functionReflection; diff --git a/src/Rules/Classes/ClassConstantRule.php b/src/Rules/Classes/ClassConstantRule.php index 4ab9d2e083..58d5b8e65e 100644 --- a/src/Rules/Classes/ClassConstantRule.php +++ b/src/Rules/Classes/ClassConstantRule.php @@ -78,7 +78,7 @@ public function processNode(Node $node, Scope $scope): array ]; } $currentClassReflection = $scope->getClassReflection(); - if ($currentClassReflection->getParentClass() === false) { + if ($currentClassReflection->getParentClass() === null) { return [ RuleErrorBuilder::message(sprintf( 'Access to parent::%s but %s does not extend any class.', diff --git a/src/Rules/Classes/InstantiationRule.php b/src/Rules/Classes/InstantiationRule.php index 71d4b62949..94237c0b6d 100644 --- a/src/Rules/Classes/InstantiationRule.php +++ b/src/Rules/Classes/InstantiationRule.php @@ -103,7 +103,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $ RuleErrorBuilder::message(sprintf('Using %s outside of class scope.', $class))->build(), ]; } - if ($scope->getClassReflection()->getParentClass() === false) { + if ($scope->getClassReflection()->getParentClass() === null) { return [ RuleErrorBuilder::message(sprintf( '%s::%s() calls new parent but %s does not extend any class.', diff --git a/src/Rules/Constants/OverridingConstantRule.php b/src/Rules/Constants/OverridingConstantRule.php index 336d42d69c..34f9bf424e 100644 --- a/src/Rules/Constants/OverridingConstantRule.php +++ b/src/Rules/Constants/OverridingConstantRule.php @@ -131,7 +131,7 @@ private function findPrototype(ClassReflection $classReflection, string $constan } $parentClass = $classReflection->getParentClass(); - if ($parentClass === false) { + if ($parentClass === null) { return null; } diff --git a/src/Rules/Generics/CrossCheckInterfacesHelper.php b/src/Rules/Generics/CrossCheckInterfacesHelper.php index 4bc11186c4..65f5e8d86f 100644 --- a/src/Rules/Generics/CrossCheckInterfacesHelper.php +++ b/src/Rules/Generics/CrossCheckInterfacesHelper.php @@ -52,7 +52,7 @@ public function check(ClassReflection $classReflection): array $parent = $classReflection->getParentClass(); $checkParents = true; - if ($first && $parent !== false) { + if ($first && $parent !== null) { $extendsTags = $classReflection->getExtendsTags(); if (!array_key_exists($parent->getName(), $extendsTags)) { $checkParents = false; @@ -60,7 +60,7 @@ public function check(ClassReflection $classReflection): array } if ($checkParents) { - while ($parent !== false) { + while ($parent !== null) { $check($parent, false); $parent = $parent->getParentClass(); } diff --git a/src/Rules/Methods/CallMethodsRule.php b/src/Rules/Methods/CallMethodsRule.php index 8007179b40..d04d600986 100644 --- a/src/Rules/Methods/CallMethodsRule.php +++ b/src/Rules/Methods/CallMethodsRule.php @@ -99,7 +99,7 @@ static function (Type $type) use ($name): bool { $referencedClass = $directClassNames[0]; $methodClassReflection = $this->reflectionProvider->getClass($referencedClass); $parentClassReflection = $methodClassReflection->getParentClass(); - while ($parentClassReflection !== false) { + while ($parentClassReflection !== null) { if ($parentClassReflection->hasMethod($name)) { return [ RuleErrorBuilder::message(sprintf( diff --git a/src/Rules/Methods/CallStaticMethodsRule.php b/src/Rules/Methods/CallStaticMethodsRule.php index cdef6c8fdf..87fda9718c 100644 --- a/src/Rules/Methods/CallStaticMethodsRule.php +++ b/src/Rules/Methods/CallStaticMethodsRule.php @@ -103,7 +103,7 @@ public function processNode(Node $node, Scope $scope): array ]; } $currentClassReflection = $scope->getClassReflection(); - if ($currentClassReflection->getParentClass() === false) { + if ($currentClassReflection->getParentClass() === null) { return [ RuleErrorBuilder::message(sprintf( '%s::%s() calls parent::%s() but %s does not extend any class.', diff --git a/src/Rules/Methods/MethodSignatureRule.php b/src/Rules/Methods/MethodSignatureRule.php index 2599c46ea2..080aa2d58d 100644 --- a/src/Rules/Methods/MethodSignatureRule.php +++ b/src/Rules/Methods/MethodSignatureRule.php @@ -129,7 +129,7 @@ private function collectParentMethods(string $methodName, ClassReflection $class $parentMethods = []; $parentClass = $class->getParentClass(); - if ($parentClass !== false && $parentClass->hasNativeMethod($methodName)) { + if ($parentClass !== null && $parentClass->hasNativeMethod($methodName)) { $parentMethod = $parentClass->getNativeMethod($methodName); if (!$parentMethod->isPrivate()) { $parentMethods[] = $parentMethod; diff --git a/src/Rules/Methods/OverridingMethodRule.php b/src/Rules/Methods/OverridingMethodRule.php index 059c21df82..56cdd11762 100644 --- a/src/Rules/Methods/OverridingMethodRule.php +++ b/src/Rules/Methods/OverridingMethodRule.php @@ -62,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array if ($prototype->getDeclaringClass()->getName() === $method->getDeclaringClass()->getName()) { if (strtolower($method->getName()) === '__construct') { $parent = $method->getDeclaringClass()->getParentClass(); - if ($parent !== false && $parent->hasConstructor()) { + if ($parent !== null && $parent->hasConstructor()) { $parentConstructor = $parent->getConstructor(); if ($parentConstructor->isFinal()->yes()) { return $this->addErrors([ diff --git a/src/Rules/Properties/AccessPropertiesRule.php b/src/Rules/Properties/AccessPropertiesRule.php index 6de912f2fe..c2d43fab36 100644 --- a/src/Rules/Properties/AccessPropertiesRule.php +++ b/src/Rules/Properties/AccessPropertiesRule.php @@ -123,7 +123,7 @@ static function (Type $type) use ($name): bool { $referencedClass = $typeResult->getReferencedClasses()[0]; $propertyClassReflection = $this->reflectionProvider->getClass($referencedClass); $parentClassReflection = $propertyClassReflection->getParentClass(); - while ($parentClassReflection !== false) { + while ($parentClassReflection !== null) { if ($parentClassReflection->hasProperty($name)) { return [ RuleErrorBuilder::message(sprintf( diff --git a/src/Rules/Properties/AccessStaticPropertiesRule.php b/src/Rules/Properties/AccessStaticPropertiesRule.php index b29beb813b..7d96f249f3 100644 --- a/src/Rules/Properties/AccessStaticPropertiesRule.php +++ b/src/Rules/Properties/AccessStaticPropertiesRule.php @@ -101,7 +101,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node, ))->build(), ]; } - if ($scope->getClassReflection()->getParentClass() === false) { + if ($scope->getClassReflection()->getParentClass() === null) { return [ RuleErrorBuilder::message(sprintf( '%s::%s() accesses parent::$%s but %s does not extend any class.', diff --git a/src/Rules/Properties/OverridingPropertyRule.php b/src/Rules/Properties/OverridingPropertyRule.php index 9f5bc22434..ad45a22d52 100644 --- a/src/Rules/Properties/OverridingPropertyRule.php +++ b/src/Rules/Properties/OverridingPropertyRule.php @@ -208,7 +208,7 @@ public function processNode(Node $node, Scope $scope): array private function findPrototype(ClassReflection $classReflection, string $propertyName): ?PhpPropertyReflection { $parentClass = $classReflection->getParentClass(); - if ($parentClass === false) { + if ($parentClass === null) { return null; } diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index d9b14ab7a3..85344d851f 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -462,7 +462,7 @@ function (\PhpParser\Node $node) use ($fileName, $lookForTrait, $traitMethodAlia if (!$traitReflection->isTrait()) { continue; } - if ($traitReflection->getFileName() === false) { + if ($traitReflection->getFileName() === null) { continue; } if (!file_exists($traitReflection->getFileName())) { diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 2214826e94..846cf7960a 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -527,7 +527,7 @@ public function toArray(): Type } $classReflection = $classReflection->getParentClass(); - } while ($classReflection !== false); + } while ($classReflection !== null); return new ConstantArrayType($arrayKeys, $arrayValues); } @@ -1092,7 +1092,7 @@ private function getParent(): ?ObjectType } $parentReflection = $thisReflection->getParentClass(); - if ($parentReflection === false) { + if ($parentReflection === null) { return null; } diff --git a/src/Type/ParserNodeTypeToPHPStanType.php b/src/Type/ParserNodeTypeToPHPStanType.php index c337f65221..4ed5f5b10a 100644 --- a/src/Type/ParserNodeTypeToPHPStanType.php +++ b/src/Type/ParserNodeTypeToPHPStanType.php @@ -31,7 +31,7 @@ public static function resolve($type, ?ClassReflection $classReflection): Type } elseif ( $lowercasedClassName === 'parent' && $classReflection !== null - && $classReflection->getParentClass() !== false + && $classReflection->getParentClass() !== null ) { $typeClassName = $classReflection->getParentClass()->getName(); } diff --git a/src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php b/src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php index 41caad5470..8dbcd52c3a 100644 --- a/src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php +++ b/src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php @@ -92,7 +92,7 @@ private function findParentClassType( ): Type { $parentClass = $classReflection->getParentClass(); - if ($parentClass === false) { + if ($parentClass === null) { return new ConstantBooleanType(false); } diff --git a/src/Type/TypehintHelper.php b/src/Type/TypehintHelper.php index 5fe0ed14fa..68b2a60e82 100644 --- a/src/Type/TypehintHelper.php +++ b/src/Type/TypehintHelper.php @@ -43,7 +43,7 @@ private static function getTypeObjectFromTypehint(string $typeString, ?string $s $broker = Broker::getInstance(); if ($selfClass !== null && $broker->hasClass($selfClass)) { $classReflection = $broker->getClass($selfClass); - if ($classReflection->getParentClass() !== false) { + if ($classReflection->getParentClass() !== null) { return new ObjectType($classReflection->getParentClass()->getName()); } } diff --git a/tests/PHPStan/Reflection/ClassReflectionTest.php b/tests/PHPStan/Reflection/ClassReflectionTest.php index 63d810a67a..fe893f3f61 100644 --- a/tests/PHPStan/Reflection/ClassReflectionTest.php +++ b/tests/PHPStan/Reflection/ClassReflectionTest.php @@ -141,7 +141,7 @@ public function testGenericInheritance(): void $this->assertSame('GenericInheritance\\C', $reflection->getDisplayName()); $parent = $reflection->getParentClass(); - $this->assertNotFalse($parent); + $this->assertNotNull($parent); $this->assertSame('GenericInheritance\\C0', $parent->getDisplayName());