Skip to content

Commit 0d30d73

Browse files
committed
Upgrade PHPStan 2
1 parent a279ba9 commit 0d30d73

File tree

6 files changed

+59
-13
lines changed

6 files changed

+59
-13
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
"require-dev": {
3535
"dg/bypass-finals": "^1.6",
3636
"friendsofphp/php-cs-fixer": "^3.0",
37+
"phpstan/phpstan": "^2.0",
38+
"phpstan/phpstan-deprecation-rules": "^2.0",
3739
"phpstan/extension-installer": "^1.3",
38-
"phpstan/phpstan": "^1.10",
39-
"phpstan/phpstan-symfony": "^1.3",
40+
"phpstan/phpstan-symfony": "^2.0",
4041
"phpunit/phpunit": "^10"
4142
},
4243
"config": {

phpstan-baseline.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ parameters:
33
-
44
message: '#no value type specified in iterable type array#'
55
path: tests/
6+
7+
# @legacy SF <= 7.4
8+
-
9+
message: '#Call to function method_exists\(\) with Symfony\\Component\\Console\\Application and .addCommand. will always evaluate to false#'

src/Command/DeployRsyncCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Ecommit\DeployRsyncBundle\Command;
1515

16+
use Ecommit\DeployRsyncBundle\DependencyInjection\Configuration;
1617
use Symfony\Component\Console\Attribute\AsCommand;
1718
use Symfony\Component\Console\Command\Command;
1819
use Symfony\Component\Console\Exception\RuntimeException;
@@ -24,15 +25,19 @@
2425
use Symfony\Component\Console\Question\ConfirmationQuestion;
2526
use Symfony\Component\Process\Process;
2627

28+
/**
29+
* @phpstan-import-type Environments from Configuration
30+
* @phpstan-import-type RsyncConfig from Configuration
31+
*/
2732
#[AsCommand(name: 'ecommit:deploy-rsync', description: 'Deploy the application with RSYNC and SSH')]
2833
final class DeployRsyncCommand extends Command
2934
{
3035
public const TARGET_FILE_REGEX = '/^file:\/\/(?<path>.+)$/';
3136
public const TARGET_SSH_REGEX = '/^ssh:\/\/(?<username>.+)@(?<hostname>[^:]+)(:(?<port>\d+)){0,1}:(?<path>.+)$/';
3237

3338
/**
34-
* @param array<string, array{target: string, rsync_options?: string[], ignore_file?: string}> $environments
35-
* @param array{rsync_path: string, rsync_options: string[], ignore_file: ?string} $rsyncConfig
39+
* @param Environments $environments
40+
* @param RsyncConfig $rsyncConfig
3641
*/
3742
public function __construct(protected array $environments, protected array $rsyncConfig, protected string $projetDir)
3843
{

src/DependencyInjection/Configuration.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1818
use Symfony\Component\Config\Definition\ConfigurationInterface;
1919

20+
/**
21+
* @phpstan-type ProcessedConfiguration array{
22+
* environments: Environments,
23+
* rsync: RsyncConfig,
24+
* }
25+
* @phpstan-type Environments array<string, array{target: string, rsync_options?: string[], ignore_file?: string}>
26+
* @phpstan-type RsyncConfig array{rsync_path: string, rsync_options: string[], ignore_file: ?string}
27+
*/
2028
class Configuration implements ConfigurationInterface
2129
{
2230
public function getConfigTreeBuilder(): TreeBuilder
@@ -31,26 +39,43 @@ public function getConfigTreeBuilder(): TreeBuilder
3139
->normalizeKeys(false)
3240
->arrayPrototype()
3341
->children()
34-
->scalarNode('target')->isRequired()
42+
->scalarNode('target')->isRequired() // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
3543
->validate()
3644
->ifTrue(fn (mixed $value) => !\is_string($value) || (!preg_match(DeployRsyncCommand::TARGET_FILE_REGEX, $value) && !preg_match(DeployRsyncCommand::TARGET_SSH_REGEX, $value)))
3745
->thenInvalid('Invalid target')
3846
->end()
3947
->end()
40-
->arrayNode('rsync_options')->prototype('scalar')->end()->end()
41-
->scalarNode('ignore_file')->end()
48+
->arrayNode('rsync_options')->prototype('scalar')->end()->end() // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
49+
->scalarNode('ignore_file') // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
50+
->validate()
51+
->ifTrue(fn (mixed $value) => !\is_string($value) && null !== $value)
52+
->thenInvalid('Invalid ignore file')
53+
->end()
54+
->end()
4255
->end()
4356
->end()
4457
->end()
4558
->arrayNode('rsync')
4659
->addDefaultsIfNotSet()
4760
->children()
48-
->scalarNode('rsync_path')->defaultValue('rsync')->end()
61+
->scalarNode('rsync_path') // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
62+
->defaultValue('rsync')
63+
->validate()
64+
->ifTrue(fn (mixed $value) => !\is_string($value))
65+
->thenInvalid('Invalid ignore file')
66+
->end()
67+
->end()
4968
->arrayNode('rsync_options')
50-
->prototype('scalar')->end()
69+
->prototype('scalar')->end() // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
5170
->defaultValue(['-azC', '--force', '--delete', '--progress'])
5271
->end()
53-
->scalarNode('ignore_file')->defaultNull()->end()
72+
->scalarNode('ignore_file') // @legacy SF < 7.2 (string node introduced in Symfony 7.2)
73+
->defaultNull()
74+
->validate()
75+
->ifTrue(fn (mixed $value) => !\is_string($value) && null !== $value)
76+
->thenInvalid('Invalid ignore file')
77+
->end()
78+
->end()
5479
->end()
5580
->end()
5681
->end()

src/DependencyInjection/EcommitDeployRsyncExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
use Symfony\Component\DependencyInjection\Extension\Extension;
1919
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2020

21+
/**
22+
* @phpstan-import-type ProcessedConfiguration from Configuration
23+
*/
2124
class EcommitDeployRsyncExtension extends Extension
2225
{
2326
public function load(array $configs, ContainerBuilder $container): void
2427
{
2528
$configuration = new Configuration();
29+
/** @var ProcessedConfiguration $config */
2630
$config = $this->processConfiguration($configuration, $configs);
2731

2832
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../config'));

tests/Command/DeployRsyncCommandTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
namespace Ecommit\DeployRsyncBundle\Tests\Command;
1515

1616
use Ecommit\DeployRsyncBundle\Command\DeployRsyncCommand;
17+
use Ecommit\DeployRsyncBundle\DependencyInjection\Configuration;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\Component\Console\Application;
1920
use Symfony\Component\Console\Exception\RuntimeException;
2021
use Symfony\Component\Console\Tester\CommandTester;
2122
use Symfony\Component\Process\Process;
2223

24+
/**
25+
* @phpstan-import-type ProcessedConfiguration from Configuration
26+
*/
2327
class DeployRsyncCommandTest extends TestCase
2428
{
2529
/**
@@ -437,7 +441,7 @@ protected function createCommandTester(array $config, array $expectedCommand, bo
437441
->getMock();
438442

439443
if (\count($expectedCommand) > 0) {
440-
$command->expects($this->once())->method('createProcess')->with($expectedCommand)->willReturnCallback(function ($command) use ($isSuccessful) {
444+
$command->expects($this->once())->method('createProcess')->with($expectedCommand)->willReturnCallback(function ($command) use ($isSuccessful) { // @phpstan-ignore-line
441445
$process = $this->getMockBuilder(Process::class)
442446
->setConstructorArgs([$command])
443447
->onlyMethods(['start', 'getIterator', 'isSuccessful'])
@@ -454,7 +458,7 @@ protected function createCommandTester(array $config, array $expectedCommand, bo
454458
return $process;
455459
});
456460
} else {
457-
$command->expects($this->never())->method('createProcess');
461+
$command->expects($this->never())->method('createProcess'); // @phpstan-ignore-line
458462
}
459463

460464
$command->setName('ecommit:deploy-rsync');
@@ -469,6 +473,9 @@ protected function createCommandTester(array $config, array $expectedCommand, bo
469473
return new CommandTester($application->find('ecommit:deploy-rsync'));
470474
}
471475

476+
/**
477+
* @return ProcessedConfiguration
478+
*/
472479
protected function getDefaultConfig(): array
473480
{
474481
return [
@@ -486,7 +493,7 @@ protected function getDefaultConfig(): array
486493
'rsync' => [
487494
'rsync_path' => 'rsync',
488495
'rsync_options' => ['-azC', '--force', '--delete', '--progress'],
489-
'ignore_file' => realpath(__DIR__.'/../ignore_file.txt'),
496+
'ignore_file' => (string) realpath(__DIR__.'/../ignore_file.txt'),
490497
],
491498
];
492499
}

0 commit comments

Comments
 (0)