Skip to content

Commit

Permalink
Merge pull request #1085 from veewee/psalm-issues
Browse files Browse the repository at this point in the history
Resolve psalm errors
  • Loading branch information
veewee committed Apr 28, 2023
2 parents bf8785b + 7b34cfb commit 82d8bf6
Show file tree
Hide file tree
Showing 68 changed files with 202 additions and 44 deletions.
3 changes: 1 addition & 2 deletions grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ grumphp:
verbose: true
functional: true
psalm:
show_info: true
no_cache: true
testsuites:
git_pre_commit:
tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser, psalm]
# On CI, we run paratest separately. For some reason this currently fails in GitHub actions.
ci:
tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser]
tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser, psalm]
# Don't run psalm on Windows for now. There is a known issue with the Windows phar:
# https://github.com/vimeo/psalm/issues/2858
windows:
Expand Down
2 changes: 1 addition & 1 deletion phive.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="psalm" version="^4.13.1" installed="4.22.0" location="./tools/psalm" copy="true"/>
<phar name="psalm" version="^5.9.0" installed="5.9.0" location="./tools/psalm" copy="true"/>
<phar name="phpcs" version="^3.6.1" installed="3.6.2" location="./tools/phpcs" copy="true"/>
<phar name="phpcbf" version="^3.6.1" installed="3.6.2" location="./tools/phpcbf" copy="true"/>
<phar name="composer-normalize" version="^2.16.0" installed="2.24.1" location="./tools/composer-normalize" copy="true"/>
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="false"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
31 changes: 23 additions & 8 deletions src/Collection/FilesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
*/
class FilesCollection extends ArrayCollection
{
/**
* @param \Traversable<array-key, \SplFileInfo> $iterator
*/
public static function fromTraversable(\Traversable $iterator): FilesCollection
{
return new self(array_values(iterator_to_array($iterator)));
}

/**
* Adds a rule that files must match.
*
Expand Down Expand Up @@ -47,7 +55,7 @@ public function names(array $patterns): self
{
$filter = new Iterator\FilenameFilterIterator($this->getIterator(), $patterns, []);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
Expand All @@ -63,7 +71,7 @@ public function notName(string $pattern): self
{
$filter = new Iterator\FilenameFilterIterator($this->getIterator(), [], [$pattern]);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
Expand All @@ -80,12 +88,14 @@ public function path(string $pattern): self
* Filter by paths.
*
* $collection->paths(['/^spec\/','/^src\/'])
*
* @psalm-suppress ArgumentTypeCoercion - Works on int, \SplFileInfo as well
*/
public function paths(array $patterns): self
{
$filter = new Iterator\PathFilterIterator($this->getIterator(), $patterns, []);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
Expand All @@ -106,12 +116,14 @@ public function notPath(string $pattern): self
* You can use patterns (delimited with / sign) or simple strings.
*
* $collection->notPaths(['/^spec\/','/^src\/'])
*
* @psalm-suppress ArgumentTypeCoercion - Works on int, \SplFileInfo as well
*/
public function notPaths(array $pattern): self
{
$filter = new Iterator\PathFilterIterator($this->getIterator(), [], $pattern);

return new self([...$filter]);
return self::fromTraversable($filter);
}

public function extensions(array $extensions): self
Expand Down Expand Up @@ -139,7 +151,7 @@ public function size(string $size): self
$comparator = new Comparator\NumberComparator($size);
$filter = new Iterator\SizeRangeFilterIterator($this->getIterator(), [$comparator]);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
Expand All @@ -161,7 +173,7 @@ public function date(string $date): self
$comparator = new Comparator\DateComparator($date);
$filter = new Iterator\DateRangeFilterIterator($this->getIterator(), [$comparator]);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
Expand All @@ -182,14 +194,17 @@ public function filter(Closure $p): self
{
$filter = new Iterator\CustomFilterIterator($this->getIterator(), [$p]);

return new self([...$filter]);
return self::fromTraversable($filter);
}

/**
* @param Traversable<array-key, SplFileInfo> $fileList
*/
public function filterByFileList(Traversable $fileList): FilesCollection
{
$allowedFiles = array_map(function (SplFileInfo $file) {
return $file->getPathname();
}, [...$fileList]);
}, iterator_to_array($fileList));

return $this->filter(function (SplFileInfo $file) use ($allowedFiles) {
return \in_array($file->getPathname(), $allowedFiles, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/GrumPHPPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function (string $argument): string {
// Loop on process until it exits normally.
do {
$status = proc_get_status($process);
} while ($status && $status['running']);
} while ($status['running']);

$exitCode = $status['exitcode'] ?? -1;
proc_close($process);
Expand Down
3 changes: 3 additions & 0 deletions src/Configuration/Compiler/ExtensionCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @psalm-suppress UndefinedDocblockClass - PHP 8.0 Doesnt know enums
*/
class ExtensionCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
Expand Down
5 changes: 5 additions & 0 deletions src/Configuration/Environment/PathsRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ private static function prependOne(string $path): void

/**
* Detect which path variable name is being used
*
* @return non-empty-string
*/
private static function pathVarName(): string
{
Expand All @@ -54,6 +56,9 @@ private static function pathVarName(): string
return $pathStr;
}

/**
* @psalm-suppress PossiblyInvalidCast - Not sure what's going on, should be always OK.
*/
private static function pathContainsDir(string $dir): bool
{
return (bool) preg_match(
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/Model/EnvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function __construct(
}

/**
* @param array{max_workers: int, enabled: bool} $config
* @psalm-suppress RedundantCastGivenDocblockType - The types are not validated here.
* @param array{files ?: list<string>, variables ?: array<string, string>, paths ?: list<string>} $config
*/
public static function fromArray(array $config): self
{
Expand Down
1 change: 1 addition & 0 deletions src/Event/Subscriber/StashUnstagedChangesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private function doSaveStash(): void
$this->io->write(['<fg=yellow>Detected unstaged changes... Stashing them!</fg=yellow>']);
}

/** @psalm-suppress RedundantCondition - it's the most clear this way.. */
if (!$hasPending && $hasUntracked) {
$this->io->write(['<fg=yellow>Detected untracked files... Stashing them!</fg=yellow>']);
}
Expand Down
1 change: 1 addition & 0 deletions src/Event/Subscriber/VerboseLoggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function onCommand(ConsoleCommandEvent $event): void
return;
}

/** @psalm-suppress DeprecatedConstant - Only possible to use enums from PHP 8.2*/
$this->logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
$this->logGuessedPaths($output);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Formatter/PhpcsFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PhpcsFormatter implements ProcessFormatterInterface
protected $output = '';

/**
* @var string[]
* @var list<string>
*/
protected $suggestedFiles = [];

Expand All @@ -41,6 +41,9 @@ public function format(Process $process): string
return $this->output;
}

/**
* @return list<string>
*/
private function getSuggestedFilesFromJson(array $json): array
{
$suggestedFiles = [];
Expand All @@ -53,7 +56,7 @@ private function getSuggestedFilesFromJson(array $json): array
}
foreach ($data['messages'] as $message) {
if (\is_array($message) && $message['fixable']) {
$suggestedFiles[] = $absolutePath;
$suggestedFiles[] = (string) $absolutePath;
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Linter/Xml/XmlLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private function validateDTD(DOMDocument $document): bool
}

// Do not validate external DTDs if the loadFromNet option is disabled:
/** @psalm-suppress UndefinedPropertyFetch - Should bet here on PHP 8.0 as well. */
$systemId = $document->doctype->systemId;
if (filter_var($systemId, FILTER_VALIDATE_URL) && !$this->loadFromNet) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/Locator/EnrichedGuessedPathsFromDotEnvLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use GrumPHP\Configuration\GuessedPaths;
use GrumPHP\Util\Filesystem;

/**
* @psalm-suppress RedundantCast - We don't want to blindly assume variables in $_SERVER are from the string type.
*/
class EnrichedGuessedPathsFromDotEnvLocator
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Locator/GuessedPathsLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use GrumPHP\Util\ComposerFile;
use GrumPHP\Util\Filesystem;

/**
* @psalm-suppress RedundantCast - We don't want to blindly assume variables in $_SERVER are from the string type.
*/
class GuessedPathsLocator
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Task/Ant.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Ant extends AbstractExternalTask
{
public static function getConfigurableOptions(): OptionsResolver
Expand Down
3 changes: 2 additions & 1 deletion src/Task/Atoum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -12,7 +13,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Atoum task.
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Atoum extends AbstractExternalTask
{
Expand Down
3 changes: 2 additions & 1 deletion src/Task/Behat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -12,7 +13,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Behat task.
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Behat extends AbstractExternalTask
{
Expand Down
3 changes: 2 additions & 1 deletion src/Task/Brunch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -12,7 +13,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Brunch task.
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Brunch extends AbstractExternalTask
{
Expand Down
3 changes: 0 additions & 3 deletions src/Task/CloverCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
use SplFileInfo;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Clover unit test coverage task.
*/
class CloverCoverage implements TaskInterface
{
/**
Expand Down
3 changes: 2 additions & 1 deletion src/Task/Codeception.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -12,7 +13,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Codeception task.
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Codeception extends AbstractExternalTask
{
Expand Down
3 changes: 3 additions & 0 deletions src/Task/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use SplFileInfo;

/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class Composer extends AbstractExternalTask
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Task/ComposerNormalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GrumPHP\Task;

use GrumPHP\Fixer\Provider\FixableProcessResultProvider;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -11,6 +12,9 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\Process;

/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class ComposerNormalize extends AbstractExternalTask
{
public function canRunInContext(ContextInterface $context): bool
Expand Down
3 changes: 2 additions & 1 deletion src/Task/ComposerRequireChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
Expand All @@ -12,7 +13,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* ComposerRequireChecker task.
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class ComposerRequireChecker extends AbstractExternalTask
{
Expand Down
Loading

0 comments on commit 82d8bf6

Please sign in to comment.