PHPStan is a static analysis tool for PHP that focuses on finding bugs in your code without running it. While PHPStan helps maintain high code quality, developers often encounter some common errors. In this guide, we’ll explore these common PHPStan errors and how to fix them.
1. “Cannot access property on null”
Error
This error occurs when PHPStan detects that you’re trying to access a property on a variable that might be null
.
Cause
PHPStan sees that a variable might be null
, but you haven’t handled this possibility.
Fix
To fix this, add a null
check before accessing the property.
Example:
// Before
$user = getUser();
echo $user->name; // Error: Cannot access property on null
// After
$user = getUser();
if ($user !== null) {
echo $user->name;
}
2. “Call to an undefined method”
Error
PHPStan reports this error when you’re calling a method that it believes doesn’t exist on the object or class.
Cause
This error is usually due to a missing import, incorrect class names, or dynamic methods that PHPStan cannot detect.
Fix
Ensure the method exists and that the correct class is imported. For dynamic methods, you can use PHPStan’s @method
annotation or extension configuration.
Example:
// Before
$user->sendEmail(); // Error: Call to an undefined method
// After
// Make sure the sendEmail method exists in the User class
For dynamic methods, use:
/**
* @method void sendEmail()
*/
class User {}
3. “Undefined variable”
Error
This error occurs when PHPStan detects that a variable is used before being defined.
Cause
The variable is either misspelled or you’re using it before initializing it.
Fix
Ensure the variable is defined before using it.
Example:
// Before
echo $total; // Error: Undefined variable $total
// After
$total = 0;
echo $total;
4. “Class not found”
Error
PHPStan throws this error when it cannot find a class that you’re trying to instantiate or reference.
Cause
This is usually caused by a missing use
statement or a typo in the class name.
Fix
Ensure that you have the correct use
statement and check for typos.
Example:
// Before
$myClass = new MyClass(); // Error: Class 'MyClass' not found
// After
use App\Classes\MyClass;
$myClass = new MyClass();
5. “Function X invoked with X parameters, X required”
Error
This error indicates that you’re passing the wrong number of arguments to a function.
Cause
You might be passing too few or too many arguments to a function or method.
Fix
Ensure that you’re passing the correct number of arguments as required by the function.
Example:
// Before
function sendMessage($message) {}
sendMessage(); // Error: Function sendMessage invoked with 0 parameters, 1 required
// After
sendMessage('Hello');
6. “Parameter type does not match”
Error
PHPStan detects that the type of the passed argument doesn’t match the expected type of the function or method.
Cause
You’re passing an argument of the wrong type.
Fix
Make sure the argument matches the expected type (e.g., passing a string when a string is expected).
Example:
// Before
function calculateSum(int $a, int $b) {}
calculateSum('1', 2); // Error: Parameter #1 $a of function calculateSum expects int, string given
// After
calculateSum(1, 2);
7. “Inaccessible method”
Error
This error is triggered when you try to call a method with the wrong visibility (e.g., trying to access a private or protected method from outside its class).
Cause
You’re attempting to call a method that is not publicly accessible.
Fix
Either make the method public
or call it from within the class where it’s defined.
Example:
// Before
class User {
private function getPassword() {}
}
$user = new User();
$user->getPassword(); // Error: Call to private method
// After
class User {
public function getPassword() {}
}
8. “Return type does not match”
Error
PHPStan detects that the type of the returned value doesn’t match the declared return type.
Cause
You’ve declared a return type for a function or method, but you’re returning a value of a different type.
Fix
Ensure that the returned value matches the declared return type.
Example:
// Before
function getTotal(): int {
return '10'; // Error: Return type mismatch
}
// After
function getTotal(): int {
return 10;
}
Mastering PHPStan: Common Errors and How to Fix Them
PHPStan is a static analysis tool for PHP that focuses on finding bugs in your code without running it. While PHPStan helps maintain high code quality, developers often encounter some common errors. In this guide, we’ll explore these common PHPStan errors and how to fix them.
1. “Cannot access property on null”
Error
This error occurs when PHPStan detects that you’re trying to access a property on a variable that might be null
.
Cause
PHPStan sees that a variable might be null
, but you haven’t handled this possibility.
Fix
To fix this, add a null
check before accessing the property.
Example:
php
Copy code
// Before
$user = getUser();
echo $user->name; // Error: Cannot access property on null
// After
$user = getUser();
if ($user !== null) {
echo $user->name;
}
2. “Call to an undefined method”
Error
PHPStan reports this error when you’re calling a method that it believes doesn’t exist on the object or class.
Cause
This error is usually due to a missing import, incorrect class names, or dynamic methods that PHPStan cannot detect.
Fix
Ensure the method exists and that the correct class is imported. For dynamic methods, you can use PHPStan’s @method
annotation or extension configuration.
Example:
php
Copy code
// Before
$user->sendEmail(); // Error: Call to an undefined method
// After
// Make sure the sendEmail method exists in the User class
For dynamic methods, use:
php
Copy code
/**
* @method void sendEmail()
*/
class User {}
3. “Undefined variable”
Error
This error occurs when PHPStan detects that a variable is used before being defined.
Cause
The variable is either misspelled or you’re using it before initializing it.
Fix
Ensure the variable is defined before using it.
Example:
php
Copy code
// Before
echo $total; // Error: Undefined variable $total
// After
$total = 0;
echo $total;
4. “Class not found”
Error
PHPStan throws this error when it cannot find a class that you’re trying to instantiate or reference.
Cause
This is usually caused by a missing use
statement or a typo in the class name.
Fix
Ensure that you have the correct use
statement and check for typos.
Example:
php
Copy code
// Before
$myClass = new MyClass(); // Error: Class 'MyClass' not found
// After
use App\Classes\MyClass;
$myClass = new MyClass();
5. “Function X invoked with X parameters, X required”
Error
This error indicates that you’re passing the wrong number of arguments to a function.
Cause
You might be passing too few or too many arguments to a function or method.
Fix
Ensure that you’re passing the correct number of arguments as required by the function.
Example:
php
Copy code
// Before
function sendMessage($message) {}
sendMessage(); // Error: Function sendMessage invoked with 0 parameters, 1 required
// After
sendMessage('Hello');
6. “Parameter type does not match”
Error
PHPStan detects that the type of the passed argument doesn’t match the expected type of the function or method.
Cause
You’re passing an argument of the wrong type.
Fix
Make sure the argument matches the expected type (e.g., passing a string when a string is expected).
Example:
php
Copy code
// Before
function calculateSum(int $a, int $b) {}
calculateSum('1', 2); // Error: Parameter #1 $a of function calculateSum expects int, string given
// After
calculateSum(1, 2);
7. “Inaccessible method”
Error
This error is triggered when you try to call a method with the wrong visibility (e.g., trying to access a private or protected method from outside its class).
Cause
You’re attempting to call a method that is not publicly accessible.
Fix
Either make the method public
or call it from within the class where it’s defined.
Example:
php
Copy code
// Before
class User {
private function getPassword() {}
}
$user = new User();
$user->getPassword(); // Error: Call to private method
// After
class User {
public function getPassword() {}
}
8. “Return type does not match”
Error
PHPStan detects that the type of the returned value doesn’t match the declared return type.
Cause
You’ve declared a return type for a function or method, but you’re returning a value of a different type.
Fix
Ensure that the returned value matches the declared return type.
Example:
php
Copy code
// Before
function getTotal(): int {
return '10'; // Error: Return type mismatch
}
// After
function getTotal(): int {
return 10;
}
9. “Constant does not exist”
Error
PHPStan throws this error when you’re referencing a constant that doesn’t exist.
Cause
This happens when you misspell a constant name or forget to define it.
Fix
Double-check the constant name or define the missing constant.
Example:
// Before
echo USER_STATUS_ACTIVE; // Error: Constant 'USER_STATUS_ACTIVE' not found
// After
define('USER_STATUS_ACTIVE', 1);
echo USER_STATUS_ACTIVE;
10. “Redundant catch clause”
Error
This error occurs when PHPStan detects that a catch
block is redundant due to previous blocks already catching the same exception.
Cause
You might be catching a generic exception type before a more specific one.
Fix
Ensure that more specific exceptions are caught before generic ones.
Example:
// Before
try {
// code
} catch (Exception $e) {
// catch all exceptions
} catch (RuntimeException $e) { // Error: Redundant catch clause
// this block is never reached
}
// After
try {
// code
} catch (RuntimeException $e) {
// handle runtime exception
} catch (Exception $e) {
// handle other exceptions
}