Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Psalm to PHPStan #155

Merged
merged 12 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/bc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- '.gitattributes'
- 'infection.json.dist'
- 'phpunit.xml.dist'
- 'psalm.xml'
- 'phpstan.neon'
push:
branches: ['master']
paths-ignore:
Expand All @@ -19,7 +19,7 @@ on:
- '.gitattributes'
- 'infection.json.dist'
- 'phpunit.xml.dist'
- 'psalm.xml'
- 'phpstan.neon'

name: backwards compatibility

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'
- 'phpstan.neon'

push:
branches: ['master']
Expand All @@ -18,7 +18,7 @@ on:
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'
- 'phpstan.neon'

name: build

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- '.gitattributes'
- 'infection.json.dist'
- 'phpunit.xml.dist'
- 'psalm.xml'
- 'phpstan.neon'

push:
branches: ['master']
Expand All @@ -20,7 +20,7 @@ on:
- '.gitattributes'
- 'infection.json.dist'
- 'phpunit.xml.dist'
- 'psalm.xml'
- 'phpstan.neon'

name: Composer require checker

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'psalm.xml'
- 'phpstan.neon'

push:
branches: ['master']
Expand All @@ -16,7 +16,7 @@ on:
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'psalm.xml'
- 'phpstan.neon'

name: mutation test

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'
- 'phpstan.neon'

name: rector

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ on:
name: static analysis

jobs:
psalm:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
phpstan:
uses: yiisoft/actions/.github/workflows/phpstan.yml@master
with:
os: >-
['ubuntu-latest']
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 5.0.1 under development

- no changes in this release.
- Chg #155: Improve static analyze annotations (@vjik)
- Enh #155: Remove unnecessary `array_filter` call in `AssetUtil::extractFilePathsForExport()` method (@vjik)

## 5.0.0 December 11, 2024

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
[![Build status](https://github.com/yiisoft/assets/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/assets/actions/workflows/build.yml)
[![Code Coverage](https://codecov.io/gh/yiisoft/assets/graph/badge.svg?token=U09s7jclX6)](https://codecov.io/gh/yiisoft/assets)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fassets%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/assets/master)
[![static analysis](https://github.com/yiisoft/assets/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/assets/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/assets/coverage.svg)](https://shepherd.dev/github/yiisoft/assets)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it missing with phpstan?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't found shepherd analog for PHPStan.

[![static analysis](https://github.com/yiisoft/assets/actions/workflows/static.yml/badge.svg)](https://github.com/yiisoft/assets/actions/workflows/static.yml)

The package implements client-side asset (such as CSS and JavaScript) management for PHP.
It helps resolve dependencies and get lists of files ready for generating HTML `<script>` and `<link>` tags.
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
"require-dev": {
"infection/infection": "^0.27.11",
"maglnet/composer-require-checker": "^4.7",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.5",
"rector/rector": "^2.0",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26",
"yiisoft/di": "^1.3",
"yiisoft/test-support": "^3.0",
"yiisoft/yii-debug": "dev-master"
Expand Down Expand Up @@ -76,6 +76,8 @@
"scripts": {
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch",
"infection": "vendor/bin/infection --only-covered"
"infection": "vendor/bin/infection --only-covered",
"phpstan": "vendor/bin/phpstan analyse",
"phpstan-clear-cache": "vendor/bin/phpstan clear-result-cache"
}
}
4 changes: 2 additions & 2 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ composer run infection

## Static analysis

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:
The code is statically analyzed with [PHPStan](https://phpstan.org/). To run static analysis:

```shell
./vendor/bin/psalm
composer run phpstan
```

## Code style
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 10
paths:
- src
ignoreErrors:
- identifier: missingType.iterableValue

includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
20 changes: 0 additions & 20 deletions psalm.xml

This file was deleted.

10 changes: 6 additions & 4 deletions src/AssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class AssetBundle
* ]
* ```
*
* @psalm-var array<array-key, string|CssFile>
* @psalm-var array<array-key, CssFile|string>
*/
public array $css = [];

Expand Down Expand Up @@ -107,7 +107,7 @@ class AssetBundle
* ];
* ```
*
* @psalm-var array<array-key, string|CssString>
* @psalm-var array<array-key, CssString|string>
*/
public array $cssStrings = [];

Expand Down Expand Up @@ -179,7 +179,7 @@ class AssetBundle
*
* Note that only a forward slash "/" should be used as directory separator.
*
* @psalm-var array<array-key, string|JsFile>
* @psalm-var array<array-key, JsFile|string>
*/
public array $js = [];

Expand Down Expand Up @@ -208,7 +208,7 @@ class AssetBundle
* ];
* ```
*
* @psalm-var array<array-key, string|JsString>
* @psalm-var array<array-key, JsString|string>
*/
public array $jsStrings = [];

Expand Down Expand Up @@ -283,6 +283,8 @@ class AssetBundle
* 'js/script.js',
* ]:
* ```
*
* @psalm-var list<string>
*/
public array $export = [];

Expand Down
13 changes: 9 additions & 4 deletions src/AssetConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
];

/**
* @var callable|null A PHP callback, which should be invoked to check whether asset conversion result is outdated.
* @var callable|null A PHP callback, which should be invoked to check whether asset conversion result is
* outdated.
*
* @psalm-var IsOutdatedCallback|null
*/
Expand All @@ -57,9 +58,9 @@
/**
* @param Aliases $aliases The aliases instance.
* @param LoggerInterface $logger The logger instance.
* @param array $commands The commands that are used to perform the asset conversion.
* The keys are the asset file extension names, and the values are the corresponding
* target script types (either "css" or "js") and the commands used for the conversion.
* @param array $commands The commands that are used to perform the asset
* conversion. The keys are the asset file extension names, and the values are the corresponding target script types
* (either "css" or "js") and the commands used for the conversion.
*
* You may also use a {@link https://github.com/yiisoft/docs/blob/master/guide/en/concept/aliases.md}
* to specify the location of the command:
Expand All @@ -78,16 +79,16 @@
private readonly Aliases $aliases,
private readonly LoggerInterface $logger,
array $commands = [],
private bool $forceConvert = false,

Check warning on line 82 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ * * @psalm-param array<string, array{0:string,1:string}> $commands */ - public function __construct(private readonly Aliases $aliases, private readonly LoggerInterface $logger, array $commands = [], private bool $forceConvert = false) + public function __construct(private readonly Aliases $aliases, private readonly LoggerInterface $logger, array $commands = [], private bool $forceConvert = true) { $this->commands = array_merge($this->commands, $commands); }
) {
$this->commands = array_merge($this->commands, $commands);

Check warning on line 84 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ */ public function __construct(private readonly Aliases $aliases, private readonly LoggerInterface $logger, array $commands = [], private bool $forceConvert = false) { - $this->commands = array_merge($this->commands, $commands); + $this->commands = $this->commands; } public function convert(string $asset, string $basePath, array $optionsConverter = []) : string {

Check warning on line 84 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ */ public function __construct(private readonly Aliases $aliases, private readonly LoggerInterface $logger, array $commands = [], private bool $forceConvert = false) { - $this->commands = array_merge($this->commands, $commands); + $this->commands = $commands; } public function convert(string $asset, string $basePath, array $optionsConverter = []) : string {
}

public function convert(string $asset, string $basePath, array $optionsConverter = []): string
{
$pos = strrpos($asset, '.');

if ($pos !== false) {

Check warning on line 91 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ public function convert(string $asset, string $basePath, array $optionsConverter = []) : string { $pos = strrpos($asset, '.'); - if ($pos !== false) { + if ($pos !== true) { $srcExt = substr($asset, $pos + 1); $commandOptions = $this->buildConverterOptions($srcExt, $optionsConverter); if (isset($this->commands[$srcExt])) {
$srcExt = substr($asset, $pos + 1);

$commandOptions = $this->buildConverterOptions($srcExt, $optionsConverter);
Expand Down Expand Up @@ -252,7 +253,7 @@

$command = $this->aliases->get($command);

$command = strtr($command, [

Check warning on line 256 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ { $basePath = $this->aliases->get($basePath); $command = $this->aliases->get($command); - $command = strtr($command, ['{options}' => $options, '{from}' => escapeshellarg("{$basePath}/{$asset}"), '{to}' => escapeshellarg("{$basePath}/{$result}")]); + $command = strtr($command, ['{from}' => escapeshellarg("{$basePath}/{$asset}"), '{to}' => escapeshellarg("{$basePath}/{$result}")]); $descriptors = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; $pipes = []; $proc = proc_open($command, $descriptors, $pipes, $basePath);
'{options}' => $options,
'{from}' => escapeshellarg("$basePath/$asset"),
'{to}' => escapeshellarg("$basePath/$result"),
Expand All @@ -266,10 +267,14 @@
$pipes = [];

$proc = proc_open($command, $descriptors, $pipes, $basePath);
/**
* @var resource $proc
* @var resource[] $pipes
*/

$stdout = stream_get_contents($pipes[1]);

Check warning on line 275 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * @var resource $proc * @var resource[] $pipes */ - $stdout = stream_get_contents($pipes[1]); + $stdout = stream_get_contents($pipes[2]); $stderr = stream_get_contents($pipes[2]); foreach ($pipes as $pipe) { fclose($pipe);

$stderr = stream_get_contents($pipes[2]);

Check warning on line 277 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * @var resource[] $pipes */ $stdout = stream_get_contents($pipes[1]); - $stderr = stream_get_contents($pipes[2]); + $stderr = stream_get_contents($pipes[1]); foreach ($pipes as $pipe) { fclose($pipe); }

foreach ($pipes as $pipe) {
fclose($pipe);
Expand All @@ -277,8 +282,8 @@

$status = proc_close($proc);

if ($status === 0) {

Check warning on line 285 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ fclose($pipe); } $status = proc_close($proc); - if ($status === 0) { + if ($status === -1) { $this->logger->debug("Converted {$asset} into {$result}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]); } else { $this->logger->error("AssetConverter command '{$command}' failed with exit code {$status}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]);

Check warning on line 285 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ fclose($pipe); } $status = proc_close($proc); - if ($status === 0) { + if ($status !== 0) { $this->logger->debug("Converted {$asset} into {$result}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]); } else { $this->logger->error("AssetConverter command '{$command}' failed with exit code {$status}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]);
$this->logger->debug(

Check warning on line 286 in src/AssetConverter.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } $status = proc_close($proc); if ($status === 0) { - $this->logger->debug("Converted {$asset} into {$result}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]); + } else { $this->logger->error("AssetConverter command '{$command}' failed with exit code {$status}:\nSTDOUT:\n{$stdout}\nSTDERR:\n{$stderr}", [__METHOD__]); }
"Converted $asset into $result:\nSTDOUT:\n$stdout\nSTDERR:\n$stderr",
[__METHOD__]
);
Expand Down
6 changes: 1 addition & 5 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,18 @@ final class AssetManager

/**
* @var true[] List of the asset bundles in register process. Use for detect circular dependency.
*
* @psalm-var array<string, true>
*/
private array $bundlesInRegisterProcess = [];

/**
* @var AssetBundle[]
*
* @psalm-var array<string, AssetBundle>
*/
private array $loadedBundles = [];

/**
* @var AssetBundle[]
*
* @psalm-var array<string, AssetBundle>
*/
private array $dummyBundles = [];
Expand Down Expand Up @@ -451,12 +448,11 @@ private function loadBundle(string $name): AssetBundle
return $this->loadedBundles[$name] = $this->loader->loadBundle($name, $this->customizedBundles[$name]);
}

/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($this->customizedBundles[$name] === false) {
/** @psalm-suppress MixedArgumentTypeCoercion */
return $this->dummyBundles[$name] ??= $this->loader->loadBundle($name, (array) (new AssetBundle()));
}

// @phpstan-ignore deadCode.unreachable
throw new InvalidConfigException("Invalid configuration of the \"{$name}\" asset bundle.");
}

Expand Down
3 changes: 1 addition & 2 deletions src/AssetPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Yiisoft\Aliases\Aliases;
use Yiisoft\Assets\Exception\InvalidConfigException;
use Yiisoft\Files\FileHelper;
use Yiisoft\Files\PathMatcher\PathMatcherInterface;

use function array_key_exists;
use function crc32;
Expand All @@ -26,7 +25,7 @@
* {@see AssetBundle::$basePath}.
*
* @psalm-type HashCallback = callable(string):string
* @psalm-type PublishedBundle = array{0:non-empty-string,1:non-empty-string}
* @psalm-import-type PublishedBundle from AssetPublisherInterface
*/
final class AssetPublisher implements AssetPublisherInterface
{
Expand Down
4 changes: 4 additions & 0 deletions src/AssetPublisherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* The `AssetPublisherInterface` must be implemented by asset publisher classes.
*
* @psalm-type PublishedBundle = array{0:non-empty-string,1:non-empty-string}
*/
interface AssetPublisherInterface
{
Expand Down Expand Up @@ -39,6 +41,8 @@ interface AssetPublisherInterface
* @throws InvalidConfigException If the asset or the asset file paths to be published does not exist.
*
* @return array The path (directory or file path) and the URL that the asset is published as.
*
* @psalm-return PublishedBundle
*/
public function publish(AssetBundle $bundle): array;

Expand Down
Loading
Loading