Skip to content

Commit

Permalink
PHPStan fixes (#1595)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Oct 19, 2021
1 parent 384d439 commit 84140e6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
27 changes: 4 additions & 23 deletions devTools/phpstan-src.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@ parameters:
path: '../src/Metrics/MetricsCalculator.php'
message: '#^Strict comparison using !== between array<int, Infection\\Mutant\\MutantExecutionResult> and array\(\) will always evaluate to true\.$#'
count: 1
-
path: '../src/Mutator/Number/DecrementInteger.php'
message: '#Casting to int .* already int#'
count: 1
-
path: '../src/PhpParser/FileParser.php'
message: '#Method Infection\\PhpParser\\FileParser::parse\(\) should return array<PhpParser\\Node> but returns array<PhpParser\\Node\\Stmt>\|null\.#'
count: 1
-
path: '../src/Reflection/AnonymousClassReflection.php'
message: '#Parameter \#1 \$argument of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object\, string given\.#'
count: 1
-
path: '../src/Reflection/CoreClassReflection.php'
message: '#Parameter \#1 \$argument of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object\, string given\.#'
count: 1
-
path: '../src/TestFramework/Coverage/XmlReport/SourceFileInfoProvider.php'
message: '#Function realpath is unsafe to use\.#'
count: 1
-
path: ../src/TestFramework/Factory.php
message: '#^Parameter \#1 \$callback of function array_map expects callable\(Infection\\TestFramework\\Coverage\\Trace\|SplFileInfo\): mixed, Closure\(SplFileInfo\): string\|false given\.$#'
Expand All @@ -48,10 +28,11 @@ parameters:
path: '../src/Container.php'
message: '#^Method Infection\\Container::get.*\(\) should return .* but returns object\.$#'
count: 1
-
path: '../src/FileSystem/DummyFileSystem.php'
message: '#Infection\\FileSystem\\DummyFileSystem#'
level: max
paths:
- ../src
excludes_analyse:
- %currentWorkingDirectory%/src/FileSystem/DummyFileSystem.php
stubFiles:
- phpstan.stub
treatPhpDocTypesAsCertain: false
10 changes: 10 additions & 0 deletions devTools/phpstan.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PhpParser\Node {
class Name {
/**
* @return class-string
*/
public function toString() {}
}
}
2 changes: 1 addition & 1 deletion src/Mutator/Number/DecrementInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function mutate(Node $node): iterable
// PHP Parser reads negative number as a pair of minus sign and a positive int,
// but positive part of PHP_INT_MIN leads to an overflow into float. To work
// around this we have to cast the result value back to int after adding one.
$value = (int) ($node->value + 1);
$value = $node->value + 1;
}

yield new Node\Scalar\LNumber($value);
Expand Down
6 changes: 3 additions & 3 deletions src/PhpParser/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

namespace Infection\PhpParser;

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Parser;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;
Expand All @@ -56,12 +56,12 @@ public function __construct(Parser $parser)
/**
* @throws UnparsableFile
*
* @return Node[]
* @return Stmt[]
*/
public function parse(SplFileInfo $fileInfo): array
{
try {
return $this->parser->parse($fileInfo->getContents());
return $this->parser->parse($fileInfo->getContents()) ?? [];
} catch (Throwable $throwable) {
$filePath = $fileInfo->getRealPath() === false
? $fileInfo->getPathname()
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/AnonymousClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private function __construct(ReflectionClass $reflectionClass)
$this->reflectionClass = $reflectionClass;
}

/**
* @param class-string $className
*/
public static function fromClassName(string $className): self
{
return new self(new ReflectionClass($className));
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/CoreClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ private function __construct(ReflectionClass $reflectionClass)
$this->reflectionClass = $reflectionClass;
}

/**
* @param class-string $className
*/
public static function fromClassName(string $className): self
{
return new self(new ReflectionClass($className));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
use function file_exists;
use function implode;
use Infection\TestFramework\SafeDOMXPath;
use function realpath as native_realpath;
use Safe\Exceptions\FilesystemException;
use function Safe\file_get_contents;
use function Safe\realpath;
use function Safe\sprintf;
use function str_replace;
use Symfony\Component\Finder\SplFileInfo;
Expand Down Expand Up @@ -130,9 +131,9 @@ private function retrieveSourceFileInfo(SafeDOMXPath $xPath): SplFileInfo
])
);

$realPath = native_realpath($path);

if ($realPath === false) {
try {
$realPath = realpath($path);
} catch (FilesystemException $e) {
$coverageFilePath = Path::canonicalize(
$this->coverageDir . DIRECTORY_SEPARATOR . $this->relativeCoverageFilePath
);
Expand Down

0 comments on commit 84140e6

Please sign in to comment.