0
0
typo3-linting-code-analyse-.../packages/example/Classes/PhpstanExample.php
2020-01-06 18:36:26 +01:00

112 lines
2.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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';
}
}