0
0

[FEATURE] Linters and code analysis for TYPO3 projects

Example file for PHPStan usage
This commit is contained in:
Felix Heller 2020-01-06 18:36:26 +01:00
parent c620f7db49
commit 10b1ce95bc
4 changed files with 161 additions and 3 deletions

View File

@ -1,6 +1,12 @@
{ {
"name": "fh/typo3-linting-code-analyse-gitlab-ci", "name": "fh/typo3-linting-code-analyse-gitlab-ci",
"description": "Bessere TYPO3 Projekte durch Linting und Code Analyse mit GitLab CI", "description": "Bessere TYPO3 Projekte durch Linting und Code Analyse mit GitLab CI",
"repositories": [
{
"type": "path",
"url": "./packages/*"
}
],
"require-dev": { "require-dev": {
"helmich/typo3-typoscript-lint": "^2.0", "helmich/typo3-typoscript-lint": "^2.0",
"phpmd/phpmd": "^2.7", "phpmd/phpmd": "^2.7",

31
composer.lock generated
View File

@ -4,8 +4,31 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "7b63ef8cdf6517e62422bb217a5595ed", "content-hash": "f6488ee2e6a7954b44ef9e076a910363",
"packages": [], "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": [ "packages-dev": [
{ {
"name": "composer/semver", "name": "composer/semver",
@ -1710,7 +1733,9 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {
"fh/example": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],

View File

@ -0,0 +1,111 @@
<?php
namespace Fh\Example;
/**
* Example of PHPStan capabilities
* See https://medium.com/@ondrejmirtes/phpstan-2939cd0ad0e3 for further information
*/
class PhpstanExample
{
/**
* Existence of classes used in instanceof, catch, typehints and other language constructs. PHP does not check this and just stays instead, rendering the surrounded code unused.
*
* @return string
*/
public function checkInstanceof(): string
{
$fooBar = new \DateTime();
if ($fooBar instanceof \Fh\Example\NonExistingClassName) {
return 'Lorem';
} else {
return 'Ipsum';
}
}
/**
* Existence and accessibility of called methods and functions. It also checks the number of passed arguments.
*
* @return void
*/
public function checkFunctionsAndNumberOfArguments(): void
{
$this->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';
}
}

View File

@ -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"
}
}
}