Skip to content

Commit 8aebce0

Browse files
committed
Add PHPStan
1 parent 188ff49 commit 8aebce0

18 files changed

+238
-34
lines changed

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ cs: $(CODE_SNIFFER) $(CODE_SNIFFER_FIX)
2525
$(PHPNOGC) $(CODE_SNIFFER_FIX) || true
2626
$(PHPNOGC) $(CODE_SNIFFER)
2727

28+
.PHONY: phpstan
29+
PHPSTAN=vendor-bin/phpstan/vendor/bin/phpstan
30+
phpstan: ## Runs PHPStan
31+
phpstan: $(PHPSTAN)
32+
$(PHPNOGC) $(PHPSTAN) analyze src -l7
33+
2834
.PHONY: build
2935
build: ## Build the PHAR
3036
BOX=bin/box
@@ -326,6 +332,10 @@ vendor-bin/code-sniffer/vendor: vendor-bin/code-sniffer/composer.lock vendor/bam
326332
composer bin code-sniffer install
327333
touch $@
328334

335+
vendor-bin/phpstan/vendor: vendor-bin/phpstan/composer.lock vendor/bamarni
336+
composer bin phpstan install
337+
touch $@
338+
329339
fixtures/set005/vendor: fixtures/set005/composer.lock
330340
composer --working-dir=fixtures/set005 install
331341
touch $@
@@ -392,6 +402,12 @@ composer.lock: composer.json
392402
vendor-bin/covers-validator/composer.lock: vendor-bin/covers-validator/composer.json
393403
@echo covers-validator composer.lock is not up to date
394404

405+
vendor-bin/code-sniffer/composer.lock: vendor-bin/code-sniffer/composer.json
406+
@echo code-sniffer composer.lock is not up to date
407+
408+
vendor-bin/phpstan/composer.lock: vendor-bin/phpstan/composer.json
409+
@echo phpstan composer.lock is not up to date
410+
395411
fixtures/set005/composer.lock: fixtures/set005/composer.json
396412
@echo fixtures/set005/composer.lock is not up to date.
397413

@@ -449,3 +465,7 @@ $(CODE_SNIFFER): vendor-bin/code-sniffer/vendor
449465
$(CODE_SNIFFER_FIX): vendor-bin/code-sniffer/vendor
450466
composer bin code-sniffer install
451467
touch $@
468+
469+
$(PHPSTAN): vendor-bin/phpstan/vendor
470+
composer bin phpstan install
471+
touch $@

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@
4040
"bin": ["bin/php-scoper"],
4141
"autoload": {
4242
"files": [
43-
"src/functions.php"
43+
"src/functions.php",
44+
"src/json.php"
4445
],
4546
"psr-4": {
4647
"Humbug\\PhpScoper\\": "src/"
4748
}
4849
},
4950
"autoload-dev": {
5051
"files": [
51-
"tests/functions.php"
52+
"tests/functions.php",
53+
"src/json.php"
5254
],
5355
"psr-4": {
5456
"Humbug\\PhpScoper\\": "tests/"

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
excludes_analyse:
3+
- src/Console/Command/AddPrefixCommand.php

src/Configuration.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Humbug\PhpScoper;
1616

17+
use function array_key_exists;
1718
use Closure;
1819
use InvalidArgumentException;
1920
use Iterator;
@@ -140,11 +141,10 @@ public static function load(string $path = null, array $paths = []): self
140141
/**
141142
* @param string|null $path Absolute path to the configuration file loaded.
142143
* @param string|null $prefix The prefix applied.
143-
* @param [string, string][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents
144+
* @param string[][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents
144145
* @param callable[] $patchers List of closures which can alter the content of the files being
145146
* scoped.
146147
* @param Whitelist $whitelist List of classes that will not be scoped.
147-
* @param Closure $globalNamespaceWhitelisters Closure taking a class name from the global namespace as an argument and
148148
* returning a boolean which if `true` means the class should be scoped
149149
* (i.e. is ignored) or scoped otherwise.
150150
* @param string[] $whitelistedFiles List of absolute paths of files to completely ignore
@@ -238,7 +238,7 @@ public function getWhitelistedFiles(): array
238238
private static function validateConfigKeys(array $config): void
239239
{
240240
array_map(
241-
['self', 'validateConfigKey'],
241+
self::class.'::validateConfigKey',
242242
array_keys($config)
243243
);
244244
}
@@ -263,7 +263,7 @@ private static function validateConfigKey(string $key): void
263263
*/
264264
private static function retrievePrefix(array $config): ?string
265265
{
266-
$prefix = array_key_exists(self::PREFIX_KEYWORD, $config) ? $config[self::PREFIX_KEYWORD] : null;
266+
$prefix = $config[self::PREFIX_KEYWORD] ?? null;
267267

268268
if (null === $prefix) {
269269
return null;
@@ -515,7 +515,7 @@ private static function retrieveFilesFromPaths(array $paths): iterable
515515
/**
516516
* @param Iterator $files
517517
*
518-
* @return [string, string][] Array of tuple with the first argument being the file path and the second its contents
518+
* @return string[][] Array of tuple with the first argument being the file path and the second its contents
519519
*/
520520
private static function retrieveFilesWithContents(Iterator $files): array
521521
{

src/Console/Command/BaseCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function configure(): void
4040

4141
final public function changeWorkingDirectory(InputInterface $input): void
4242
{
43+
/** @var string|null $workingDir */
4344
$workingDir = $input->getOption(self::WORKING_DIR_OPT);
4445

4546
if (null === $workingDir) {

src/Console/Command/InitCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104104

105105
private function retrieveConfig(InputInterface $input, OutputStyle $io): ?string
106106
{
107+
/** @var string|null $configFile */
107108
$configFile = $input->getOption(self::CONFIG_FILE_OPT);
108109

109110
$configFile = (null === $configFile)

src/PhpParser/NodeTraverser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace Humbug\PhpScoper\PhpParser;
1616

17+
use function array_slice;
18+
use function array_values;
19+
use function count;
1720
use PhpParser\Node;
1821
use PhpParser\Node\Name;
1922
use PhpParser\Node\Stmt\Declare_;
@@ -84,6 +87,13 @@ public function traverse(array $nodes): array
8487
*/
8588
private function wrapInNamespace(array $nodes): array
8689
{
90+
if ([] === $nodes) {
91+
return $nodes;
92+
}
93+
94+
$nodes = array_values($nodes);
95+
96+
$firstRealStatementIndex = 0;
8797
$realStatements = [];
8898

8999
foreach ($nodes as $i => $node) {

src/PhpParser/NodeVisitor/Collection/NamespaceStmtCollection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class NamespaceStmtCollection implements IteratorAggregate, Countable
3737
private $nodes = [];
3838

3939
/**
40-
* @var Name|null[] Associative array with the potentially prefixed namespace names as keys and their original name
40+
* @var (Name|null)[] Associative array with the potentially prefixed namespace names as keys and their original name
4141
* as value.
4242
*/
4343
private $mapping = [];
@@ -80,11 +80,9 @@ public function findNamespaceByName(string $name): ?Name
8080

8181
public function getCurrentNamespaceName(): ?Name
8282
{
83-
if (0 === count($this->nodes)) {
84-
return null;
85-
}
83+
$lastNode = end($this->nodes);
8684

87-
return end($this->nodes)->name;
85+
return false === $lastNode ? null : $lastNode->name;
8886
}
8987

9088
/**

src/PhpParser/NodeVisitor/Collection/UseStmtCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function findStatementForNode(?Name $namespaceName, Name $node): ?Name
8585

8686
foreach ($useStatements as $use_) {
8787
foreach ($use_->uses as $useStatement) {
88-
if (false === ($useStatement instanceof UseUse)) {
88+
if (!($useStatement instanceof UseUse)) {
8989
continue;
9090
}
9191

src/PhpParser/NodeVisitor/ConstStmtReplacer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver;
1818
use Humbug\PhpScoper\Whitelist;
1919
use PhpParser\Node;
20+
use PhpParser\Node\Arg;
21+
use PhpParser\Node\Expr;
2022
use PhpParser\Node\Expr\FuncCall;
2123
use PhpParser\Node\Name;
2224
use PhpParser\Node\Name\FullyQualified;
@@ -89,8 +91,10 @@ public function enterNode(Node $node): Node
8991
new FuncCall(
9092
new FullyQualified('define'),
9193
[
92-
new String_((string) $resolvedConstantName),
93-
$constant->value,
94+
new Arg(
95+
new String_((string) $resolvedConstantName)
96+
),
97+
new Arg($constant->value),
9498
]
9599
)
96100
);

0 commit comments

Comments
 (0)