Skip to content

Commit 8feb2ae

Browse files
authored
Enable PHPStan on tests (#659)
1 parent 2fad29c commit 8feb2ae

15 files changed

+236
-22
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ cs-check: $(CODE_SNIFFER)
4848
PHPSTAN=vendor-bin/phpstan/vendor/bin/phpstan
4949
phpstan: ## Runs PHPStan
5050
phpstan: $(PHPSTAN)
51-
$(PHPNOGC) $(PHPSTAN) analyze src --level max --memory-limit=-1
51+
$(PHPNOGC) $(PHPSTAN) analyze src tests --level max --memory-limit=-1
5252

5353
.PHONY: build
5454
build: ## Builds the PHAR

phpstan.neon.dist

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ parameters:
22
checkMissingIterableValueType: false
33
checkGenericClassInNonGenericObjectType: false
44

5-
excludePaths:
6-
- src/Console/Command/AddPrefixCommand.php
5+
excludePaths: []
76

87
ignoreErrors:
98
- message: '#Cannot cast array\<string\>\|string to string\.#'
@@ -24,4 +23,17 @@ parameters:
2423
path: 'src/PhpParser/NodeVisitor/OriginalNameResolver.php'
2524
- message: '#NamespaceManipulator::getOriginalName\(\) should return#'
2625
path: 'src/PhpParser/NodeVisitor/NamespaceStmt/NamespaceManipulator.php'
27-
# TODO: this one is really strange and may be worth investigating
26+
- message: '#Dead catch#'
27+
path: 'tests/Scoper/PhpScoperTest.php'
28+
- message: '#Dead catch#'
29+
path: 'tests/Scoper/PhpScoperSpecTest.php'
30+
- message: '#Cannot assign offset#'
31+
path: 'tests/Scoper/PhpScoperSpecTest.php'
32+
- message: '#DummyScoperFactory extends @final#'
33+
path: 'tests/Console/Command/DummyScoperFactory.php'
34+
- message: '#Anonymous function should return string but returns array#'
35+
path: 'tests/Console/Command/AddPrefixCommandIntegrationTest.php'
36+
- message: '#AddPrefixCommandIntegrationTest\:\:getNormalizeDisplay\(\) should return string but returns array#'
37+
path: 'tests/Console/Command/AddPrefixCommandIntegrationTest.php'
38+
- message: '#ConfigurationKeysTest\:\:retrieveConfigurationKeys\(\) should return array#'
39+
path: 'tests/Configuration/ConfigurationKeysTest.php'

src/Console/Command/AddPrefixCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Humbug\PhpScoper\Console\ConfigLoader;
2727
use Humbug\PhpScoper\Console\ConsoleScoper;
2828
use Humbug\PhpScoper\Scoper\ScoperFactory;
29+
use InvalidArgumentException;
2930
use Symfony\Component\Console\Exception\RuntimeException;
3031
use Symfony\Component\Console\Input\InputArgument;
3132
use Symfony\Component\Console\Input\InputOption;
@@ -158,6 +159,9 @@ public function execute(IO $io): int
158159
return ExitCode::SUCCESS;
159160
}
160161

162+
/**
163+
* @return non-empty-string
164+
*/
161165
private function getOutputDir(IO $io, string $cwd): string
162166
{
163167
return $this->canonicalizePath(
@@ -260,11 +264,17 @@ private function getPathArguments(IO $io, string $cwd): array
260264
*/
261265
private function canonicalizePath(string $path, string $cwd): string
262266
{
263-
return Path::canonicalize(
267+
$canonicalPath = Path::canonicalize(
264268
$this->fileSystem->isAbsolutePath($path)
265269
? $path
266270
: $cwd.DIRECTORY_SEPARATOR.$path,
267271
);
272+
273+
if ('' === $canonicalPath) {
274+
throw new InvalidArgumentException('Cannot canonicalize empty path and empty working directory');
275+
}
276+
277+
return $canonicalPath;
268278
}
269279

270280
private function getScoper(): ConsoleScoper

tests/Configuration/ConfigurationKeysTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ private static function retrieveKeywords(): array
5050
{
5151
$configKeysReflection = new ReflectionClass(ConfigurationKeys::class);
5252

53-
$constant = $configKeysReflection->getConstant('KEYWORDS');
53+
$constants = $configKeysReflection->getConstant('KEYWORDS');
5454

55-
foreach ($constant as $index => $value) {
55+
self::assertIsArray($constants);
56+
57+
foreach ($constants as $index => $value) {
5658
self::assertNonEmptyStringConstantValue(
5759
$value,
5860
(string) $index,
5961
);
6062
}
6163

62-
return $constant;
64+
return $constants;
6365
}
6466

6567
/**

tests/Configuration/ConfigurationSymbolsConfigurationFactoryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Humbug\PhpScoper\Symbol\SymbolRegistry;
99
use InvalidArgumentException;
1010
use PHPUnit\Framework\TestCase;
11+
use Throwable;
1112

1213
/**
1314
* @covers \Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory
@@ -184,6 +185,8 @@ public static function configProvider(): iterable
184185

185186
/**
186187
* @dataProvider invalidConfigProvider
188+
*
189+
* @param class-string<Throwable> $expectedExceptionClassName
187190
*/
188191
public function test_it_cannot_create_a_whitelist_from_an_invalid_config(
189192
array $config,

tests/Configuration/ConfigurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ final class ConfigurationTest extends TestCase
2727
{
2828
/**
2929
* @dataProvider prefixProvider
30+
*
31+
* @param non-empty-string $prefix
3032
*/
3133
public function test_it_validates_the_prefix(
3234
string $prefix,

tests/Console/Command/AddPrefixCommandIntegrationTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use function Safe\preg_replace;
3131
use function Safe\realpath;
3232
use function str_replace;
33+
use function Safe\sprintf;
3334
use const DIRECTORY_SEPARATOR;
3435

3536
/**
@@ -329,9 +330,19 @@ private static function collectFiles(string $dir): array
329330
return array_reduce(
330331
iterator_to_array($files),
331332
static function (array $collectedFiles, SplFileInfo $file) use ($dir): array {
332-
$path = str_replace($dir, '', $file->getRealPath());
333+
$realPath = $file->getRealPath();
333334

334-
$collectedFiles[$path] = file_get_contents($file->getRealPath());
335+
self::assertIsString(
336+
$realPath,
337+
sprintf(
338+
'Expected file "%s" to have a real path.',
339+
$file->getPathname()
340+
),
341+
);
342+
343+
$path = str_replace($dir, '', $realPath);
344+
345+
$collectedFiles[$path] = file_get_contents($realPath);
335346

336347
return $collectedFiles;
337348
},

tests/PhpParser/UseStmtNameTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ private static function parseUseStmtName(string $php): Name
196196

197197
$statements = $parser->parse($php);
198198

199+
self::assertNotNull($statements);
200+
199201
$traverser->traverse($statements);
200202

201203
foreach ($useStatements as $useStmts) {

tests/PhpScoperAssertions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class PhpScoperAssertions
1212
{
1313
/**
1414
* @parma list $expected
15+
* @param mixed $actual
1516
*/
1617
public static function assertListEqualsCanonicalizing(
1718
array $expected,

tests/Scoper/PhpScoperSpecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private static function parseSpecFile(string $file, array $meta, $fixtureTitle,
330330
$spec = sprintf(
331331
'[%s] %s',
332332
$meta['title'],
333-
isset($fixtureSet['spec']) ? $fixtureSet['spec'] : $fixtureTitle
333+
$fixtureSet['spec'] ?? $fixtureTitle
334334
);
335335

336336
$payload = is_string($fixtureSet) ? $fixtureSet : $fixtureSet['payload'];

0 commit comments

Comments
 (0)