1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/docs/issues.md

1437 lines
24 KiB
Markdown
Raw Normal View History

2017-12-12 06:49:40 +01:00
# Issue types
### AbstractInstantiation
Emitted when an attempt is made to instatiate an abstract class:
```php
abstract class A {}
new A();
```
### AssignmentToVoid
Emitted when assigning from a function that returns `void`:
```php
function foo() : void {}
$a = foo();
```
2018-01-01 02:32:47 +01:00
### CircularReference
Emitted when a class references itself as one of its parents
```php
class A extends B {}
class B extends A {}
```
2017-12-12 06:49:40 +01:00
### ContinueOutsideLoop
Emitted when encountering a `continue` statement outside a loop context.
### DeprecatedClass
Emitted when creating a new instance of a deprecated class:
```php
/** @deprecated */
class A {}
new A();
```
### DeprecatedMethod
Emitted when calling a deprecated method on a given class:
```php
class A {
/** @deprecated */
public function foo() : void {}
}
(new A())->foo();
```
### DeprecatedProperty
Emitted when getting/setting a deprecated property of a given class
```php
class A {
/**
* @deprecated
* @var ?string
*/
public $foo;
}
(new A())->foo = 5;
```
### DuplicateClass
Emitted when a class is defined twice
```php
class A {}
class A {}
```
### DuplicateParam
Emitted when a class param is defined twice
```php
class A {
/** @var ?string */
public $foo;
/** @var ?string */
public $foo;
}
```
### EmptyArrayAccess
Emitted when attempting to access a value on an empty array
```php
$a = [];
$b = $a[0];
```
### ForbiddenCode
Emitted when Psalm encounters a var_dump, exec or similar expression that may make your code more vulnerable
```php
var_dump($foo);
```
### ImplementedReturnTypeMismatch
Emitted when a class that inherits another, or implements an interface, has docblock return type that's entirely different to the parent. Subclasses of the parent return type are permitted, in docblocks.
```php
class A {
/** @return bool */
public function foo() {
return true;
}
}
class B extends A {
/** @return string */
public function foo() {
return true;
}
}
```
### ImplicitToStringCast
Emitted when implictly converting an object with a `__toString` method to a string
```php
class A {
public function __toString() {
return "foo";
}
}
function takesString(string $s) : void {}
takesString(new A);
```
### InaccessibleClassConstant
Emitted when a public/private class constant is not accessible from the calling context
```php
class A {
protected const FOO = 'FOO';
}
echo A::FOO;
```
### InaccessibleMethod
2017-12-12 07:16:51 +01:00
Emitted when attempting to access a protected/private method from outside its available scope
2017-12-12 06:49:40 +01:00
```php
2017-12-12 07:16:51 +01:00
class A {
protected function foo() : void {}
}
echo (new A)->foo();
2017-12-12 06:49:40 +01:00
```
### InaccessibleProperty
2017-12-12 07:16:51 +01:00
Emitted when attempting to access a protected/private property from outside its available scope
2017-12-12 06:49:40 +01:00
```php
2017-12-12 07:16:51 +01:00
class A {
/** @return string */
protected $foo;
}
echo (new A)->foo;
2017-12-12 06:49:40 +01:00
```
### InvalidArgument
2017-12-12 07:16:51 +01:00
Emitted when a supplied function/method argument is incompatible with the method signature or docblock one.
2017-12-12 06:49:40 +01:00
```php
2017-12-12 07:16:51 +01:00
class A {}
function foo(A $a) : void {}
foo("hello");
2017-12-12 06:49:40 +01:00
```
### InvalidArrayAccess
2017-12-12 07:16:51 +01:00
Emitted when attempting to access an array offset on a value that does not permit it
2017-12-12 06:49:40 +01:00
```php
2017-12-12 07:16:51 +01:00
$arr = 5;
echo $arr[0];
2017-12-12 06:49:40 +01:00
```
### InvalidArrayAssignment
2017-12-13 17:57:46 +01:00
Emitted when attempting to assign a value on a non-array
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$arr = 5;
$arr[0] = 3;
2017-12-12 06:49:40 +01:00
```
### InvalidArrayOffset
2017-12-13 17:57:46 +01:00
Emitted when when attempting to access an array using a value that's not a valid offet for that array
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$a = [5, 20, 18];
echo $a["hello"];
2017-12-12 06:49:40 +01:00
```
### InvalidCast
2017-12-13 17:57:46 +01:00
Emitted when attempting to cast a value that's not castable
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
class A {}
$a = new A();
$b = (string)$a;
2017-12-12 06:49:40 +01:00
```
2018-01-01 02:32:47 +01:00
### InvalidCatch
Emitted when trying to catch a class/interface that doesn't extend `Exception` or implement `Throwable`
```php
class A {}
try {
$worked = true;
}
catch (A $e) {}
```
2017-12-12 06:49:40 +01:00
### InvalidClass
2017-12-13 17:57:46 +01:00
Emitted when referencing a class with the wrong casing
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
class Foo {}
(new foo());
2017-12-12 06:49:40 +01:00
```
### InvalidClone
2017-12-13 17:57:46 +01:00
Emitted when trying to clone a value that's not cloneable
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$a = "hello";
$b = clone $a;
2017-12-12 06:49:40 +01:00
```
### InvalidDocblock
2017-12-13 17:57:46 +01:00
Emitted when there's an error in a docblock type
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
/** @var array() */
$a = [];
2017-12-12 06:49:40 +01:00
```
### InvalidFunctionCall
2017-12-13 17:57:46 +01:00
Emitted when calling a function on a non-callable variable
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$a = 5;
$b = $a();
2017-12-12 06:49:40 +01:00
```
### InvalidGlobal
2017-12-22 17:56:21 +01:00
Emitted when there's a reference to the global keyword where it's not expected
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
global $e;
2017-12-12 06:49:40 +01:00
```
### InvalidIterator
2017-12-13 17:57:46 +01:00
Emitted when trying to iterate over a value that's not iterable
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$a = 5;
foreach ($a as $b) {}
2017-12-12 06:49:40 +01:00
```
### InvalidMethodCall
2017-12-13 17:57:46 +01:00
Emitted when attempting to call a method on a non-object
2017-12-12 06:49:40 +01:00
```php
2017-12-13 17:57:46 +01:00
$a = 5;
$a->foo();
2017-12-12 06:49:40 +01:00
```
### InvalidOperand
2017-12-22 17:56:21 +01:00
Emitted when using something as an operand that is unexected
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {}
echo (new A) . ' ';
2017-12-12 06:49:40 +01:00
```
### InvalidParamDefault
2017-12-22 17:56:21 +01:00
Emitted when a function parameter default clashes with the type Psalm expects the param to be
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo(int $i = false) : void {}
2017-12-12 06:49:40 +01:00
```
### InvalidPassByReference
2017-12-22 17:56:21 +01:00
Emitted when passing a non-variable to a function that expects a by-ref variable
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo(array &$arr) : void {}
foo([0, 1, 2]);
2017-12-12 06:49:40 +01:00
```
### InvalidPropertyAssignment
2017-12-22 17:56:21 +01:00
Emitted when attempting to assign a property to a non-object
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
$a = "foo";
$a->bar = "bar";
2017-12-12 06:49:40 +01:00
```
### InvalidPropertyFetch
2017-12-22 17:56:21 +01:00
Emitted when attempting to get a property from a non-object
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
$a = "foo";
echo $a->bar;
2017-12-12 06:49:40 +01:00
```
### InvalidReturnStatement
2017-12-22 17:56:21 +01:00
Emitted when a function return statement is incorrect
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo() : string {
return 5; // emitted here
}
2017-12-12 06:49:40 +01:00
```
### InvalidReturnType
2017-12-22 17:56:21 +01:00
Emitted when a functions return type is incorrect (often emitted with `InvalidReturnStatement`)
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo() : string {
if (rand(0, 1)) {
return "foo";
}
}
2017-12-12 06:49:40 +01:00
```
### InvalidScalarArgument
2017-12-22 17:56:21 +01:00
Emitted when a scalar value is passed to a method that expected another scalar type
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo(int $i) : void {}
function bar(string $s) : void {
if (is_numeric($s)) {
foo($s);
}
}
2017-12-12 06:49:40 +01:00
```
### InvalidScope
2017-12-22 17:56:21 +01:00
Emitted when referring to `$this` outside a class
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
echo $this;
2017-12-12 06:49:40 +01:00
```
### InvalidStaticInvocation
2017-12-22 17:56:21 +01:00
Emitted when trying to call an instance function statically
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {
/** @var ?string */
public $foo;
2017-12-12 06:49:40 +01:00
2017-12-22 17:56:21 +01:00
public function bar() : void {
echo $this->foo;
}
}
2017-12-12 06:49:40 +01:00
2017-12-22 17:56:21 +01:00
A::bar();
2017-12-12 06:49:40 +01:00
```
2018-01-01 02:32:47 +01:00
### InvalidThrow
Emitted when trying to throw a class that doesn't extend `Exception` or implement `Throwable`
```php
class A {}
throw new A();
```
2017-12-12 06:49:40 +01:00
### InvalidToString
2017-12-22 17:56:21 +01:00
Emitted when a `__toString` method does not always return a `string`
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {
public function __toString() {
return true;
}
}
2017-12-12 06:49:40 +01:00
```
### LessSpecificReturnStatement
2017-12-22 17:56:21 +01:00
Emitted when a return statement is more general than the return type given for the function
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {}
class B extends A {}
2017-12-12 06:49:40 +01:00
2017-12-22 17:56:21 +01:00
function foo() : B {
return new A(); // emitted here
}
2017-12-12 06:49:40 +01:00
```
### LessSpecificReturnType
2017-12-22 17:56:21 +01:00
Emitted when a return type covers more possibilities than the function itself
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo() : ?int {
return 5;
}
```
### LoopInvalidation
2017-12-12 06:49:40 +01:00
2017-12-22 17:56:21 +01:00
Emitted when logic inside a loop invalidates one of the conditionals of the loop
```php
for ($i = 0; $i < 10; $i++) {
$i = 5;
}
2017-12-12 06:49:40 +01:00
```
### MethodSignatureMismatch
2017-12-22 17:56:21 +01:00
Emitted when a method parameter differs from a parent method parameter, or if there are fewer parameters than the parent method
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {
public function foo(int $i) : void {}
}
class B extends A {
public function foo(string $s) : void {}
}
2017-12-12 06:49:40 +01:00
```
### MisplacedRequiredParam
2017-12-22 17:56:21 +01:00
Emitted when a required param is before a param that is not required. Included in Psalm because it is an E_WARNING in PHP
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo(int $i = 5, string $j) : void {}
2017-12-12 06:49:40 +01:00
```
### MissingClosureReturnType
2017-12-22 17:56:21 +01:00
Emitted when a closure lacks a return type
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
$a = function() {
return "foo";
};
2017-12-12 06:49:40 +01:00
```
### MissingConstructor
2017-12-22 17:56:21 +01:00
Emitted when non-null properties without default values are defined in a class without a `__construct` method
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {
/** @var string */
public $foo;
}
2017-12-12 06:49:40 +01:00
```
### MissingDocblockType
2017-12-22 17:56:21 +01:00
Emitted when a docblock is present, but the type is missing or badly formatted
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
/** @var $a */
$a = [];
2017-12-12 06:49:40 +01:00
```
### MissingFile
2017-12-22 17:56:21 +01:00
Emitted when using `include` or `require` on a file that does not exist
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
require("nonexistent.php");
2017-12-12 06:49:40 +01:00
```
### MissingPropertyType
2017-12-22 17:56:21 +01:00
Emitted when a property is defined on a class without a type
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
class A {
public $foo;
}
2017-12-12 06:49:40 +01:00
```
### MissingReturnType
2017-12-22 17:56:21 +01:00
Emitted when a function doesn't have a return type defined
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function foo() {
return "foo";
}
2017-12-12 06:49:40 +01:00
```
### MixedArgument
2017-12-22 17:56:21 +01:00
Emitted when Psalm cannot determine the type of an argument
2017-12-12 06:49:40 +01:00
```php
2017-12-22 17:56:21 +01:00
function takesInt(int $i) : void {}
2017-12-24 01:11:08 +01:00
takesInt($_GET['foo']);
2017-12-12 06:49:40 +01:00
```
### MixedArrayAccess
2017-12-22 17:56:21 +01:00
Emitted when trying to access an array offset on a value whose type Psalm cannot determine
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
echo $_GET['foo'][0];
2017-12-12 06:49:40 +01:00
```
### MixedArrayAssignment
2017-12-22 17:56:21 +01:00
Emitted when trying to assign a value to an array offset on a value whose type Psalm cannot determine
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
$_GET['foo'][0] = "5";
2017-12-12 06:49:40 +01:00
```
### MixedArrayOffset
2017-12-22 17:56:21 +01:00
Emitted when attempting to access an array offset where Psalm cannot determine the offset type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
echo [1, 2, 3][$_GET['foo']];
2017-12-12 06:49:40 +01:00
```
### MixedAssignment
2017-12-31 19:13:12 +01:00
Emitted when assigning a variable to a value for which Psalm cannot infer a type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
$a = $_GET['foo'];
2017-12-12 06:49:40 +01:00
```
### MixedInferredReturnType
2017-12-24 01:11:08 +01:00
Emitted when Psalm cannot determine a function's return type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
function foo() : int {
return $_GET['foo'];
}
2017-12-12 06:49:40 +01:00
```
### MixedMethodCall
2017-12-24 01:11:08 +01:00
Emitted when calling a method on a value that Psalm cannot infer a type for
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
/** @param mixed $a */
function foo($a) : void {
$a->foo();
}
2017-12-12 06:49:40 +01:00
```
### MixedOperand
2017-12-24 01:11:08 +01:00
Emitted when Psalm cannot infer a type for an operand in any calculated expression
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
echo $_GET['foo'] + "hello";
2017-12-12 06:49:40 +01:00
```
### MixedPropertyAssignment
2017-12-31 19:13:12 +01:00
Emitted when assigning a property to a value for which Psalm cannot infer a type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
/** @param mixed $a */
function foo($a) : void {
$a->foo = "bar";
}
2017-12-12 06:49:40 +01:00
```
### MixedPropertyFetch
2017-12-31 19:13:12 +01:00
Emitted when retrieving a property on a value for which Psalm cannot infer a type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
/** @param mixed $a */
function foo($a) : void {
echo $a->foo;
}
2017-12-12 06:49:40 +01:00
```
### MixedStringOffsetAssignment
2017-12-24 01:11:08 +01:00
Emitted when assigning a value on a string using a value for which Psalm cannot infer a type
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
"hello"[$_GET['foo']] = "h";
2017-12-12 06:49:40 +01:00
```
### MixedTypeCoercion
2017-12-24 01:11:08 +01:00
Emitted when Psalm cannot be sure that part of an array/iterabble argument's type constraints can be fulfilled
2017-12-12 06:49:40 +01:00
```php
2017-12-24 01:11:08 +01:00
function foo(array $a) : void {
takesStringArray($a);
}
2017-12-12 06:49:40 +01:00
2017-12-24 01:11:08 +01:00
/** @param string[] $a */
function takesStringArray(array $a) : void {}
2017-12-12 06:49:40 +01:00
```
### MoreSpecificImplementedReturnType
2017-12-31 19:13:12 +01:00
Emitted when a class implements an interface method but its return type is less specific than the interface method return type
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
class A {}
class B extends A {}
interface I {
/** @return B[] */
public function foo();
}
class D implements I {
/** @return A[] */
public function foo() {
return [new A, new A];
}
}
2017-12-12 06:49:40 +01:00
```
### MoreSpecificReturnType
2017-12-31 19:13:12 +01:00
Emitted when the declared return type for a method is more specific than the inferred one (emitted in the same methods that `LessSpecificReturnStatement` is)
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
class A {}
class B extends A {}
function foo() : B {
/** @psalm-suppress LessSpecificReturnStatement */
return new A();
}
2017-12-12 06:49:40 +01:00
```
### NoInterfaceProperties
2017-12-31 19:13:12 +01:00
Emitted when trying to fetch a property on an interface as interfaces, by definition, do not have definitions for properties.
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
interface I {}
class A implements I {
/** @var ?string */
public $foo;
}
function bar(I $i) : void {
if ($i->foo) {}
}
2017-12-12 06:49:40 +01:00
```
### NonStaticSelfCall
Emitted when
```php
```
### NullArgument
2017-12-31 19:13:12 +01:00
Emitted when calling a function with a null value argument when the function does not expect it
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
function foo(string $s) : void {}
foo(null);
2017-12-12 06:49:40 +01:00
```
### NullArrayAccess
2017-12-31 19:13:12 +01:00
Emitted when trying to access an array value on `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$arr = null;
echo $arr[0];
2017-12-12 06:49:40 +01:00
```
### NullArrayOffset
2017-12-31 19:13:12 +01:00
Emitted when trying to access an array offset with `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$arr = ['' => 5, 'foo' => 1];
echo $arr[null];
2017-12-12 06:49:40 +01:00
```
### NullFunctionCall
2017-12-31 19:13:12 +01:00
Emitted when trying to use `null` as a `callable`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$arr = null;
echo $arr();
2017-12-12 06:49:40 +01:00
```
### NullIterator
2017-12-31 19:13:12 +01:00
Emitted when iterating over `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
foreach (null as $a) {}
2017-12-12 06:49:40 +01:00
```
### NullOperand
2017-12-31 19:13:12 +01:00
Emitted when using `null` as part of an operation (e.g. `+`, `.`, `^` etc.`)
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
echo null . 'hello';
2017-12-12 06:49:40 +01:00
```
### NullPropertyAssignment
2017-12-31 19:13:12 +01:00
Emitted when trying to set a property on `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$a = null;
$a->foo = "bar";
2017-12-12 06:49:40 +01:00
```
### NullPropertyFetch
2017-12-31 19:13:12 +01:00
Emitted when trying to fetch a property on a `null` value
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$a = null;
echo $a->foo;
2017-12-12 06:49:40 +01:00
```
### NullReference
2017-12-31 19:13:12 +01:00
Emitted when attempting to call a method on `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
$a = null;
$a->foo();
2017-12-12 06:49:40 +01:00
```
### OverriddenMethodAccess
2017-12-31 19:13:12 +01:00
Emitted when a method is less accessible than its parent
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
class A {
public function foo() : void {}
}
class B extends A {
protected function foo() : void {}
}
2017-12-12 06:49:40 +01:00
```
### ParadoxicalCondition
2017-12-31 19:13:12 +01:00
Emitted when a paradox is encountered in your programs logic that could not be caught by `RedundantCondition`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
function foo(?string $a) : ?string {
if ($a) return $a;
if ($a) echo "cannot happen";
}
2017-12-12 06:49:40 +01:00
```
### ParentNotFound
Emitted when using `parent::` in a class without a parent class.
2017-12-12 06:49:40 +01:00
```php
class A {
public function foo() : void {
parent::foo();
}
}
2017-12-12 06:49:40 +01:00
```
### PossiblyFalseArgument
Emitted when a function argument is possibly `false`, but the function doesnt expect `false`. This is distinct from a function argument is possibly `bool`, which results in `PossiblyInvalidArgument`.
2017-12-12 06:49:40 +01:00
```php
function foo(string $s) : void {
$a_pos = strpos($s, "a");
echo substr($s, $a_pos);
}
2017-12-12 06:49:40 +01:00
```
### PossiblyFalseReference
Emitted when making a method call on a value than might be `false`
2017-12-12 06:49:40 +01:00
```php
class A {
public function bar() : void {}
}
2017-12-12 06:49:40 +01:00
/** @return A|false */
function foo() {
return rand(0, 1) ? new A : false;
}
foo()->bar();
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidArgument
Emitted when
```php
/** @return string|int */
function foo() {
return rand(0, 1) ? 5 : "i";
}
function bar(int $i) : void {}
bar(foo());
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidArrayAccess
Emitted when attempting to access an array offset on a value that may not be an array
2017-12-12 06:49:40 +01:00
```php
$arr = rand(0, 1) ? 5 : [4, 3, 2, 1];
echo $arr[0];
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidArrayAssignment
Emitted when attempting to assign an array offset on a value that may not be an array
2017-12-12 06:49:40 +01:00
```php
$arr = rand(0, 1) ? 5 : [4, 3, 2, 1];
$arr[0] = "hello";
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidArrayOffset
Emitted when its possible that the array offset is not applicable to the value youre trying to access.
2017-12-12 06:49:40 +01:00
```php
$arr = rand(0, 5) > 2 ? ["a" => 5] : "hello";
echo $arr[0];
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidMethodCall
Emitted when trying to call a method on a value that may not be an object
2017-12-12 06:49:40 +01:00
```php
class A {
public function bar() : void {}
}
2017-12-12 06:49:40 +01:00
/** @return A|int */
function foo() {
return rand(0, 1) ? new A : 5;
}
foo()->bar();
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidPropertyAssignment
Emitted when trying to assign a property on a value that may not be an object or may be an object that doesnt have the desired property.
2017-12-12 06:49:40 +01:00
```php
class A {
/** @var ?string */
public $bar;
}
2017-12-12 06:49:40 +01:00
/** @return A|int */
function foo() {
return rand(0, 1) ? new A : 5;
}
$a = foo();
$a->bar = "5";
2017-12-12 06:49:40 +01:00
```
### PossiblyInvalidPropertyFetch
Emitted when trying to fetch a property on a value that may not be an object or may be an object that doesnt have the desired property.
2017-12-12 06:49:40 +01:00
```php
class A {
/** @var ?string */
public $bar;
}
/** @return A|int */
function foo() {
return rand(0, 1) ? new A : 5;
}
2017-12-12 06:49:40 +01:00
$a = foo();
echo $a->bar;
2017-12-12 06:49:40 +01:00
```
### PossiblyNullArgument
Emitted when calling a function with a value thats possibly null when the function does not expect it
2017-12-12 06:49:40 +01:00
```php
function foo(string $s) : void {}
foo(rand(0, 1) ? "hello" : null);
2017-12-12 06:49:40 +01:00
```
### PossiblyNullArrayAccess
Emitted when trying to access an array offset on a possibly null value
2017-12-12 06:49:40 +01:00
```php
public function foo(?array $a) : void {
echo $a[0];
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullArrayAssignment
Emitted when trying to set a value on a possibly null array
2017-12-12 06:49:40 +01:00
```php
public function foo(?array $a) : void {
$a[0] = "5";
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullArrayOffset
Emitted when trying to access a value on an array using a possibly null offset
2017-12-12 06:49:40 +01:00
```php
public function foo(?int $a) : void {
echo [1, 2, 3, 4][$a];
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullFunctionCall
Emitted when trying to call a function on a value that may be null
2017-12-12 06:49:40 +01:00
```php
public function foo(?callable $a) : void {
$a();
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullIterator
Emitted when trying to iterate over a value that may be null
2017-12-12 06:49:40 +01:00
```php
public function foo(?array $arr) : void {
foreach ($arr as $a) {}
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullOperand
Emitted when using a possibly `null` value as part of an operation (e.g. `+`, `.`, `^` etc.`)
2017-12-12 06:49:40 +01:00
```php
public function foo(?int $a) : void {
echo $a + 5;
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullPropertyAssignment
Emitted when trying to assign a property to a possibly null object
2017-12-12 06:49:40 +01:00
```php
class A {
/** @var ?string */
public $foo;
}
function foo(?A $a) : void {
$a->foo = "bar";
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullPropertyFetch
Emitted when trying to fetch a property on a possibly null object
2017-12-12 06:49:40 +01:00
```php
class A {
/** @var ?string */
public $foo;
}
function foo(?A $a) : void {
echo $a->foo;
}
2017-12-12 06:49:40 +01:00
```
### PossiblyNullReference
Emitted when trying to call a method on a possibly null value
2017-12-12 06:49:40 +01:00
```php
class A {
public function bar() : void {}
}
function foo(?A $a) : void {
$a->bar();
}
2017-12-12 06:49:40 +01:00
```
### PossiblyUndefinedGlobalVariable
Emitted when trying to access a variable in the global scope that may not be defined
2017-12-12 06:49:40 +01:00
```php
if (rand(0, 1)) {
$a = 5;
}
echo $a;
2017-12-12 06:49:40 +01:00
```
### PossiblyUndefinedMethod
Emitted when trying to access a method that may not be defined on the object
2017-12-12 06:49:40 +01:00
```php
class A {
public function bar() : void {}
}
class B {}
2017-12-12 06:49:40 +01:00
$a = rand(0, 1) ? new A : new B;
$a->bar();
2017-12-12 06:49:40 +01:00
```
### PossiblyUndefinedVariable
Emitted when trying to access a variable in function scope that may not be defined
2017-12-12 06:49:40 +01:00
```php
function foo() : void {
if (rand(0, 1)) {
$a = 5;
}
echo $a;
}
2017-12-12 06:49:40 +01:00
```
### PossiblyUnusedMethod
Emitted when `--find-dead-code` is turned on and Psalm cannot find any calls to a given class method
2017-12-12 06:49:40 +01:00
```php
class A {
public function foo() : void {}
public function bar() : void {}
}
(new A)->foo();
2017-12-12 06:49:40 +01:00
```
2018-01-01 02:32:47 +01:00
### PossiblyUnusedParam
2017-12-12 06:49:40 +01:00
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a particular parameter in a public/protected method
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {
public function foo(int $a, int $b) : int {
return $a + 4;
}
}
2017-12-12 06:49:40 +01:00
```
### PropertyNotSetInConstructor
2017-12-31 23:32:35 +01:00
Emitted when a non-null property without a default value is declared but not set in the classs constructor
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
class A {
/** @var string */
public $foo;
2017-12-12 06:49:40 +01:00
2017-12-31 23:32:35 +01:00
public function __construct() {}
}
2017-12-12 06:49:40 +01:00
```
### RawObjectIteration
2017-12-31 23:32:35 +01:00
Emitted when iterating over an objects properties. This issue exists because it may be undesired behaviour (e.g. you may have meant to iterate over an array)
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
class A {
/** @var string */
public $foo;
/** @var string */
public $bar;
}
2017-12-12 06:49:40 +01:00
2017-12-31 23:32:35 +01:00
function takesA(A $a) {
foreach ($a as $property) {}
}
2017-12-12 06:49:40 +01:00
```
### RedundantCondition
2017-12-31 19:13:12 +01:00
Emitted when conditional is redundant given previous assertions
2017-12-12 06:49:40 +01:00
```php
2017-12-31 19:13:12 +01:00
class A {}
function foo(?A $a) : ?A {
if ($a) return $a;
if ($a) echo "cannot happen";
}
2017-12-12 06:49:40 +01:00
```
### ReferenceConstraintViolation
2017-12-31 23:32:35 +01:00
Emitted when changing the type of a pass-by-reference variable
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
function foo(string &$a) {
$a = 5;
}
2017-12-12 06:49:40 +01:00
```
### ReservedWord
2017-12-31 23:32:35 +01:00
Emitted when using a reserved word as a class name
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
function foo(resource $res) : void {}
2017-12-12 06:49:40 +01:00
```
### TooFewArguments
2017-12-31 23:32:35 +01:00
Emitted when calling a function with fewer arguments than the function has parameters
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
function foo(string $a) : void {}
foo();
2017-12-12 06:49:40 +01:00
```
### TooManyArguments
2017-12-31 23:32:35 +01:00
Emitted when calling a function with more arguments than the function has parameters
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
function foo(string $a) : void {}
foo("hello", 4);
2017-12-12 06:49:40 +01:00
```
### TypeCoercion
2017-12-31 23:32:35 +01:00
Emitted when calling a function with an argument which has a less specific type than the function expects
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
class A {}
class B extends A {}
2017-12-12 06:49:40 +01:00
2017-12-31 23:32:35 +01:00
function takesA(A $a) : void {
takesB($a);
}
function takesB(B $b) : void {}
2017-12-12 06:49:40 +01:00
```
### TypeDoesNotContainNull
2017-12-31 23:32:35 +01:00
Emitted when checking a non-nullable type for `null`
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
$a = "hello";
if ($a === null) {}
2017-12-12 06:49:40 +01:00
```
### TypeDoesNotContainType
2017-12-31 23:32:35 +01:00
Emitted checking whether one value has a type or value that is impossible given its currently-known type
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
$a = "hello";
if ($a === 5) {}
2017-12-12 06:49:40 +01:00
```
### UndefinedClass
2017-12-31 23:32:35 +01:00
Emitted when referencing a class that doesnt exist
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
$a = new A();
2017-12-12 06:49:40 +01:00
```
### UndefinedConstant
2017-12-31 23:32:35 +01:00
Emitted when referencing a constant that doesnt exist
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
echo FOO_BAR;
2017-12-12 06:49:40 +01:00
```
### UndefinedFunction
2017-12-31 23:32:35 +01:00
Emitted when referencing a function that doesn't exist
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
foo();
2017-12-12 06:49:40 +01:00
```
### UndefinedGlobalVariable
2017-12-31 23:32:35 +01:00
Emitted when referencing a variable that doesn't exist
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
echo $a;
2017-12-12 06:49:40 +01:00
```
### UndefinedMethod
2017-12-31 23:32:35 +01:00
Emitted when calling a method that doesnt exist
2017-12-12 06:49:40 +01:00
```php
2017-12-31 23:32:35 +01:00
class A {}
A::foo();
2017-12-12 06:49:40 +01:00
```
### UndefinedPropertyAssignment
2018-01-01 02:32:47 +01:00
Emitted when assigning a property on an object that doesnt have that property defined
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {}
$a = new A();
$a->foo = "bar";
2017-12-12 06:49:40 +01:00
```
### UndefinedPropertyFetch
2018-01-01 02:32:47 +01:00
Emitted when getting a property on an object that doesnt have that property defined
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {}
$a = new A();
echo $a->foo;
2017-12-12 06:49:40 +01:00
```
### UndefinedThisPropertyAssignment
2018-01-01 02:32:47 +01:00
Emitted when assigning a property on an object in one of that objects methods when no such property exists
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {
function foo() {
$this->foo = "bar";
}
}
2017-12-12 06:49:40 +01:00
```
### UndefinedThisPropertyFetch
2018-01-01 02:32:47 +01:00
Emitted when getting a property for an object in one of that objects methods when no such property exists
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {
function foo() {
echo $this->foo;
}
}
2017-12-12 06:49:40 +01:00
```
### UndefinedTrait
2018-01-01 02:32:47 +01:00
Emitted when referencing a trait that doesnt exist
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {
use T;
}
2017-12-12 06:49:40 +01:00
```
### UndefinedVariable
2018-01-01 02:32:47 +01:00
Emitted when referencing a variable that doesn't exist in a given functions scope
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
function foo() {
echo $a;
}
2017-12-12 06:49:40 +01:00
```
### UnevaluatedCode
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm encounters code that will not be evaluated
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
function foo() : void {
return;
$a = "foo";
}
2017-12-12 06:49:40 +01:00
```
### UnimplementedAbstractMethod
2018-01-01 02:32:47 +01:00
Emitted when a class extends another, but does not implement all of its abstract methods
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
abstract class A {
abstract public function foo();
}
class B extends A {}
2017-12-12 06:49:40 +01:00
```
### UnimplementedInterfaceMethod
2018-01-01 02:32:47 +01:00
Emitted when a class `implements` an interface but does not implement all of its methods
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
interface I {
public function foo();
}
class A implements I {}
2017-12-12 06:49:40 +01:00
```
### UnrecognizedExpression
2018-01-01 02:32:47 +01:00
Emitted when Psalm encounters an expression that it doesn't know how to handle. This should never happen.
2017-12-12 06:49:40 +01:00
### UnrecognizedStatement
2018-01-01 02:32:47 +01:00
Emitted when Psalm encounters a code construct that it doesn't know how to handle. This should never happen.
2017-12-12 06:49:40 +01:00
### UnresolvableInclude
2018-01-01 02:32:47 +01:00
Emitted when Psalm cannot figure out what specific file is being included/required by PHP.
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
function requireFile(string $s) : void {
require_once($s);
}
2017-12-12 06:49:40 +01:00
```
### UntypedParam
2018-01-01 02:32:47 +01:00
Emitted when a function paramter has no type information associated with it
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
function foo($a) : void {}
2017-12-12 06:49:40 +01:00
```
### UnusedClass
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a given class
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {}
class B {}
$a = new A();
2017-12-12 06:49:40 +01:00
```
### UnusedMethod
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a given private method or function
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
class A {
public function __construct() {
$this->foo();
}
private function foo() : void {}
private function bar() : void {}
}
new A();
```
### UnusedParam
2017-12-12 06:49:40 +01:00
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a particular parameter in a private method or function
```php
function foo(int $a, int $b) : int {
return $a + 4;
}
2017-12-12 06:49:40 +01:00
```
### UnusedVariable
2018-01-01 02:32:47 +01:00
Emitted when `--find-dead-code` is turned on and Psalm cannot find any references to a variable, once instantiated
2017-12-12 06:49:40 +01:00
```php
2018-01-01 02:32:47 +01:00
function foo() : void {
$a = 5;
$b = 4;
echo $b;
}
2017-12-12 06:49:40 +01:00
```