From 10b1ce95bc2a30fe8d8c63057e391fbcb858e0ac Mon Sep 17 00:00:00 2001 From: Felix Heller Date: Mon, 6 Jan 2020 18:36:26 +0100 Subject: [PATCH] [FEATURE] Linters and code analysis for TYPO3 projects Example file for PHPStan usage --- composer.json | 6 ++ composer.lock | 31 +++++- packages/example/Classes/PhpstanExample.php | 111 ++++++++++++++++++++ packages/example/composer.json | 16 +++ 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 packages/example/Classes/PhpstanExample.php create mode 100644 packages/example/composer.json diff --git a/composer.json b/composer.json index e6d8d07..2bb03f8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,12 @@ { "name": "fh/typo3-linting-code-analyse-gitlab-ci", "description": "Bessere TYPO3 Projekte durch Linting und Code Analyse mit GitLab CI", + "repositories": [ + { + "type": "path", + "url": "./packages/*" + } + ], "require-dev": { "helmich/typo3-typoscript-lint": "^2.0", "phpmd/phpmd": "^2.7", diff --git a/composer.lock b/composer.lock index 3981a3f..3d4b2dd 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,31 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7b63ef8cdf6517e62422bb217a5595ed", - "packages": [], + "content-hash": "f6488ee2e6a7954b44ef9e076a910363", + "packages": [ + { + "name": "fh/example", + "version": "dev-master", + "dist": { + "type": "path", + "url": "./packages/example", + "reference": "12075429739dc484d9945813e3a41087a0227dcf" + }, + "type": "typo3-cms-extension", + "autoload": { + "psr-4": { + "Fh\\Example\\": "Classes" + } + }, + "authors": [ + { + "name": "Felix Heller", + "email": "felix.heller@web.de" + } + ], + "description": "Example" + } + ], "packages-dev": [ { "name": "composer/semver", @@ -1710,7 +1733,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "fh/example": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": [], diff --git a/packages/example/Classes/PhpstanExample.php b/packages/example/Classes/PhpstanExample.php new file mode 100644 index 0000000..45589f9 --- /dev/null +++ b/packages/example/Classes/PhpstanExample.php @@ -0,0 +1,111 @@ +anotherMethod('Foo', 'Bar', 'Lorem'); + } + + /** + * @param string $foo + * @param string $bar + * @return string + */ + protected function anotherMethod(string $foo, string $bar): string + { + return 'Foo: ' . $foo . ', Bar: ' . $bar; + } + + /** + * Whether a method returns the same type it declares to return. + * + * @return integer + */ + public function checkReturnType(): int + { + return 'Lorem'; + } + + /** + * Existence and visibility of accessed properties. It will also point out if a different type from the declared one is assigned to the property. + * + * @return void + */ + public function checkTypeOfDeclaredProperties(): void + { + $this->anotherMethod('Foo', false); + } + + /** + * Correct number of parameters passed to sprintf/printf calls based on format strings. + * + * @return void + */ + public function checkVariableScopes(): void + { + $foo = 'Foo'; + $exampleFunction = function (): string { + return $foo; + }; + } + + /** + * Useless casting like (string) ‘foo’ and strict comparisons (=== and !==) with different types as operands which always result in false. + * + * @return boolean + */ + public function checkUselessTypeComparison(): bool + { + $lorem = 'Lorem'; + return ($lorem === 1); + } + + /** + * @return int[] + */ + public function checkTypeOfArrayVariables(): array + { + return [ + 'Foo', + 'Bar', + ]; + } + + /** + * @return string + */ + public function checkUnreachableStatement(): string + { + $lorem = 'Lorem'; + return $lorem; + + $ipsum = 'Ipsum'; + } +} diff --git a/packages/example/composer.json b/packages/example/composer.json new file mode 100644 index 0000000..17df855 --- /dev/null +++ b/packages/example/composer.json @@ -0,0 +1,16 @@ +{ + "name": "fh/example", + "description": "Example", + "type": "typo3-cms-extension", + "authors": [ + { + "name": "Felix Heller", + "email": "felix.heller@web.de" + } + ], + "autoload": { + "psr-4": { + "Fh\\Example\\": "Classes" + } + } +}