Menu

← Back to parameter.*

Error Identifier: parameter.requiredAfterOptional

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function doFoo(int $a = 1, string $b): void
{
}

Why is it reported? #

A required parameter follows an optional parameter. This has been deprecated in PHP 8.0 and will cause a deprecation notice at runtime. The optional parameter’s default value can never actually be used because the required parameter after it forces the caller to always provide all arguments up to that point.

Since PHP 8.1, this also applies to nullable parameters with a null default value (e.g., ?int $foo = null).

How to fix it #

Reorder the parameters so that required parameters come before optional ones:

-function doFoo(int $a = 1, string $b): void
+function doFoo(string $b, int $a = 1): void
 {
 }

Alternatively, make the first parameter required if the default value is not needed:

-function doFoo(int $a = 1, string $b): void
+function doFoo(int $a, string $b): void
 {
 }

If both parameters should be optional, give the second one a default value too:

-function doFoo(int $a = 1, string $b): void
+function doFoo(int $a = 1, string $b = ''): void
 {
 }

How to ignore this error #

You can use the identifier parameter.requiredAfterOptional to ignore this error using a comment:

// @phpstan-ignore parameter.requiredAfterOptional
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: parameter.requiredAfterOptional

Rules that report this error #

  • PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule [1]
  • PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule [1]
  • PHPStan\Rules\Functions\ExistingClassesInTypehintsRule [1]
  • PHPStan\Rules\Methods\ExistingClassesInTypehintsRule [1]
  • PHPStan\Rules\Properties\ExistingClassesInPropertyHookTypehintsRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.