[FEATURE] Linters and code analysis for TYPO3 projects
Example file for PHPStan usage
This commit is contained in:
111
packages/example/Classes/PhpstanExample.php
Normal file
111
packages/example/Classes/PhpstanExample.php
Normal 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';
|
||||
}
|
||||
}
|
16
packages/example/composer.json
Normal file
16
packages/example/composer.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user