112 lines
2.6 KiB
PHP
112 lines
2.6 KiB
PHP
|
<?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';
|
|||
|
}
|
|||
|
}
|