Skip to content

Commit

Permalink
[BCB] Changed return types of various methods that had |false to |null
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 23, 2021
1 parent a321a3e commit 629ccf6
Show file tree
Hide file tree
Showing 39 changed files with 110 additions and 145 deletions.
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Analyser/FileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public function getFileDescription(): string
}

$traitReflection = $this->context->getTraitReflection();
if ($traitReflection->getFileName() === false) {
if ($traitReflection->getFileName() === null) {
throw new \PHPStan\ShouldNotHappenException();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Dependency/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Dependency/NodeDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private static function getParentReflections(ClassReflection $classReflection):
$result = [];

$parent = $classReflection->getParentClass();
if ($parent !== false) {
if ($parent !== null) {
$result[] = $parent;
}

Expand Down
4 changes: 2 additions & 2 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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();

}
Expand Down
7 changes: 3 additions & 4 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -326,7 +325,7 @@ private function getCustomFunction(string $functionName): \PHPStan\Reflection\Ph
$isDeprecated,
$isInternal,
$isFinal,
$reflectionFunction->getFileName(),
$reflectionFunction->getFileName() !== false ? $reflectionFunction->getFileName() : null,
$isPure
);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Reflection/ClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
54 changes: 22 additions & 32 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TypeAlias>|null */
private ?array $typeAliases = null;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -575,7 +564,7 @@ public function getParents(): array
{
$parents = [];
$parent = $this->getParentClass();
while ($parent !== false) {
while ($parent !== null) {
$parents[] = $parent;
$parent = $parent->getParentClass();
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/FunctionReflectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -33,7 +33,7 @@ public function create(
bool $isDeprecated,
bool $isInternal,
bool $isFinal,
$filename,
?string $filename,
?bool $isPure = null
): PhpFunctionReflection;

Expand Down
15 changes: 3 additions & 12 deletions src/Reflection/Php/BuiltinMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 629ccf6

Please sign in to comment.