From 6e140f84f7567c6e996ddb4d392b2ef29997fa33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Thu, 16 Jan 2025 21:42:05 +0100 Subject: [PATCH] :lipstick: Ajout phpstan + rector --- .../{php-lint.yml => php-cs-fixer.yml} | 0 .gitea/workflows/phpstan.yml | 15 + .gitea/workflows/rector.yml | 12 + .gitignore | 4 + composer.json | 5 + composer.lock | 286 +++++++++++++++++- phpstan.dist.neon | 14 + rector.php | 32 ++ src/Command/CreateUserCommand.php | 4 +- src/Controller/AdminController.php | 2 +- src/Controller/FilesController.php | 5 +- src/Controller/SecurityController.php | 2 +- src/Entity/User.php | 2 +- src/Twig/Runtime/BasenameExtensionRuntime.php | 2 +- src/Twig/Runtime/SizeExtensionRuntime.php | 2 +- symfony.lock | 12 + 16 files changed, 388 insertions(+), 11 deletions(-) rename .gitea/workflows/{php-lint.yml => php-cs-fixer.yml} (100%) create mode 100644 .gitea/workflows/phpstan.yml create mode 100644 .gitea/workflows/rector.yml create mode 100644 phpstan.dist.neon create mode 100644 rector.php diff --git a/.gitea/workflows/php-lint.yml b/.gitea/workflows/php-cs-fixer.yml similarity index 100% rename from .gitea/workflows/php-lint.yml rename to .gitea/workflows/php-cs-fixer.yml diff --git a/.gitea/workflows/phpstan.yml b/.gitea/workflows/phpstan.yml new file mode 100644 index 0000000..9bfe38e --- /dev/null +++ b/.gitea/workflows/phpstan.yml @@ -0,0 +1,15 @@ +name: CI + +on: [push] + +jobs: + build-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: php-actions/composer@v6 + - uses: php-actions/phpstan@v3 + with: + php_version: 8.2 + diff --git a/.gitea/workflows/rector.yml b/.gitea/workflows/rector.yml new file mode 100644 index 0000000..e23bc8a --- /dev/null +++ b/.gitea/workflows/rector.yml @@ -0,0 +1,12 @@ +on: [push, pull_request] +name: rector +jobs: + rector: + name: Rector + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Rector + uses: zingimmick/rector-action@0.0.3 + with: + args: --dry-run diff --git a/.gitignore b/.gitignore index 40d4dde..88a27ed 100755 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ /.php-cs-fixer.php /.php-cs-fixer.cache ###< friendsofphp/php-cs-fixer ### + +###> phpstan/phpstan ### +phpstan.neon +###< phpstan/phpstan ### diff --git a/composer.json b/composer.json index 0569034..f0d4330 100755 --- a/composer.json +++ b/composer.json @@ -58,6 +58,7 @@ "config": { "allow-plugins": { "php-http/discovery": true, + "phpstan/extension-installer": true, "symfony/flex": true, "symfony/runtime": true }, @@ -108,7 +109,11 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.68", + "phpstan/extension-installer": "^1.4", + "phpstan/phpstan-strict-rules": "^2.0", + "phpstan/phpstan-symfony": "^2.0", "phpunit/phpunit": "^11.5.2", + "rector/rector": "^2.0", "symfony/browser-kit": "7.2.*", "symfony/css-selector": "7.2.*", "symfony/debug-bundle": "7.2.*", diff --git a/composer.lock b/composer.lock index 9be6064..15ee70a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "40b87990e9b847fe9cac632caac98af7", + "content-hash": "ad6bf07457863ff03fcd219a3f64fd2e", "packages": [ { "name": "composer/semver", @@ -9273,6 +9273,231 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/85e90b3942d06b2326fba0403ec24fe912372936", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0 || ^2.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.3" + }, + "time": "2024-09-04T20:21:43+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "2.1.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "cd6e973e04b4c2b94c86e8612b5a65f0da0e08e7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/cd6e973e04b4c2b94c86e8612b5a65f0da0e08e7", + "reference": "cd6e973e04b4c2b94c86e8612b5a65f0da0e08e7", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2025-01-05T16:43:48+00:00" + }, + { + "name": "phpstan/phpstan-strict-rules", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-strict-rules.git", + "reference": "ed6fea0ad4ad9c7e25f3ad2e7c4d420cf1e67fe3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/ed6fea0ad4ad9c7e25f3ad2e7c4d420cf1e67fe3", + "reference": "ed6fea0ad4ad9c7e25f3ad2e7c4d420cf1e67fe3", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0", + "phpstan/phpstan": "^2.0.4" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpunit/phpunit": "^9.6" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Extra strict and opinionated rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/2.0.1" + }, + "time": "2024-12-12T20:21:10+00:00" + }, + { + "name": "phpstan/phpstan-symfony", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-symfony.git", + "reference": "c08cd8e54a08d651bc402d304cfa161c3c3766c4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-symfony/zipball/c08cd8e54a08d651bc402d304cfa161c3c3766c4", + "reference": "c08cd8e54a08d651bc402d304cfa161c3c3766c4", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "php": "^7.4 || ^8.0", + "phpstan/phpstan": "^2.0" + }, + "conflict": { + "symfony/framework-bundle": "<3.0" + }, + "require-dev": { + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-phpunit": "^2.0", + "phpstan/phpstan-strict-rules": "^2.0", + "phpunit/phpunit": "^9.6", + "psr/container": "1.0 || 1.1.1", + "symfony/config": "^5.4 || ^6.1", + "symfony/console": "^5.4 || ^6.1", + "symfony/dependency-injection": "^5.4 || ^6.1", + "symfony/form": "^5.4 || ^6.1", + "symfony/framework-bundle": "^5.4 || ^6.1", + "symfony/http-foundation": "^5.4 || ^6.1", + "symfony/messenger": "^5.4", + "symfony/polyfill-php80": "^1.24", + "symfony/serializer": "^5.4", + "symfony/service-contracts": "^2.2.0" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon", + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Lukáš Unger", + "email": "looky.msc@gmail.com", + "homepage": "https://lookyman.net" + } + ], + "description": "Symfony Framework extensions and rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-symfony/issues", + "source": "https://github.com/phpstan/phpstan-symfony/tree/2.0.1" + }, + "time": "2025-01-04T13:58:15+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "11.0.8", @@ -10223,6 +10448,65 @@ ], "time": "2024-06-11T12:45:25+00:00" }, + { + "name": "rector/rector", + "version": "2.0.6", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "fa0cb009dc3df084bf549032ae4080a0481a2036" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/fa0cb009dc3df084bf549032ae4080a0481a2036", + "reference": "fa0cb009dc3df084bf549032ae4080a0481a2036", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0", + "phpstan/phpstan": "^2.1.1" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "suggest": { + "ext-dom": "To manipulate phpunit.xml via the custom-rule command" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/2.0.6" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2025-01-06T10:38:36+00:00" + }, { "name": "sebastian/cli-parser", "version": "3.0.2", diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..9b39675 --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,14 @@ +parameters: + level: 5 + paths: + - src + excludePaths: + - tests + symfony: + containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml + strictRules: + noVariableVariables: false + + reportUnmatchedIgnoredErrors: false + treatPhpDocTypesAsCertain: false + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..f0b2ac4 --- /dev/null +++ b/rector.php @@ -0,0 +1,32 @@ +symfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml'); + + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_82, + SymfonySetList::SYMFONY_71, + SymfonySetList::SYMFONY_CODE_QUALITY, + SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION, + SetList::DEAD_CODE, + SetList::CODE_QUALITY, + SetList::PHP_82, + DoctrineSetList::DOCTRINE_CODE_QUALITY, + ]); + $rectorConfig->paths([ + __DIR__ . '/src', + __DIR__ . '/tests', + __DIR__ . '/migrations', + ]); + + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); +}; diff --git a/src/Command/CreateUserCommand.php b/src/Command/CreateUserCommand.php index 4ccf160..64b1902 100755 --- a/src/Command/CreateUserCommand.php +++ b/src/Command/CreateUserCommand.php @@ -39,7 +39,7 @@ class CreateUserCommand extends Command try { $user = $this->userRepository->findOneBy(['email' => $email]); - if ($user) { + if ($user !== null) { $io->error('Un utilisateur existe déjà avec cet email'); return Command::FAILURE; } @@ -54,7 +54,7 @@ class CreateUserCommand extends Command $this->entityManager->flush(); $io->success('Utilisateur créé avec succès'); - } catch (\Exception $e) { + } catch (\Exception) { $io->error('Une erreur est survenue lors de la création de l\'utilisateur'); } diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index ad6260a..05edad4 100755 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -47,7 +47,7 @@ class AdminController extends AbstractController public function editUsers(#[MapEntity(id: 'user')] ?User $user, Request $request): Response { $isNew = false; - if (!$user) { + if (!$user instanceof \App\Entity\User) { $user = new User(); $isNew = true; } diff --git a/src/Controller/FilesController.php b/src/Controller/FilesController.php index 501a574..d3b7a36 100755 --- a/src/Controller/FilesController.php +++ b/src/Controller/FilesController.php @@ -42,7 +42,7 @@ class FilesController extends AbstractController $realFiles = []; foreach ($files as $file) { - $filename = basename($file['path']); + $filename = basename((string) $file['path']); if (!str_starts_with($filename, '.')) { $realFiles[] = [ 'type' => $file['type'], @@ -301,8 +301,7 @@ class FilesController extends AbstractController $path = trim($path, '/'); // On retire les chemins relatifs $path = str_replace('..', '', $path); - $path = str_replace('//', '/', $path); - return $path; + return str_replace('//', '/', $path); } } diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 1684e2f..47b6e7b 100755 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -12,7 +12,7 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; class SecurityController extends AbstractController { - public function __construct(private readonly UrlGeneratorInterface $urlGenerator) + public function __construct() { } diff --git a/src/Entity/User.php b/src/Entity/User.php index c54d773..9424af3 100755 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -38,7 +38,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface public function initId(): void { - if (null !== $this->id) { + if ($this->id instanceof \Symfony\Component\Uid\Uuid) { return; } diff --git a/src/Twig/Runtime/BasenameExtensionRuntime.php b/src/Twig/Runtime/BasenameExtensionRuntime.php index 2ee1fa1..4e8bbda 100755 --- a/src/Twig/Runtime/BasenameExtensionRuntime.php +++ b/src/Twig/Runtime/BasenameExtensionRuntime.php @@ -10,6 +10,6 @@ class BasenameExtensionRuntime implements RuntimeExtensionInterface { public function basename($value) { - return \basename($value); + return \basename((string) $value); } } diff --git a/src/Twig/Runtime/SizeExtensionRuntime.php b/src/Twig/Runtime/SizeExtensionRuntime.php index 62afbd7..4cbf193 100755 --- a/src/Twig/Runtime/SizeExtensionRuntime.php +++ b/src/Twig/Runtime/SizeExtensionRuntime.php @@ -17,7 +17,7 @@ class SizeExtensionRuntime implements RuntimeExtensionInterface { $bytes = $value; $size = ['B', 'KB', 'MB', 'GB','TB']; - $factor = floor((strlen($bytes) - 1) / 3); + $factor = floor((strlen((string) $bytes) - 1) / 3); return sprintf('%.1f', $bytes / 1024 ** $factor) . ' ' . @$size[$factor]; } diff --git a/symfony.lock b/symfony.lock index 1d7b7cd..005b052 100755 --- a/symfony.lock +++ b/symfony.lock @@ -50,6 +50,18 @@ "config/packages/oneup_flysystem.yaml" ] }, + "phpstan/phpstan": { + "version": "2.1", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "1.0", + "ref": "5e490cc197fb6bb1ae22e5abbc531ddc633b6767" + }, + "files": [ + "phpstan.dist.neon" + ] + }, "phpunit/phpunit": { "version": "9.6", "recipe": {