scalarint|float|string|bool — the four types that are not arrays, objects, null
or resources. PHPStan reads the bare word as a class first and only falls
back to the union when no such class is in scope, so the spelling is one of
the few whose meaning depends on what else the file declares.
References:
- PHPStan TypeNodeResolver scalar resolves to int|float|string|bool, after trying a pseudo-type class| Analyzer | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Argument 1 ($value) is [1,2] of type array{0:1,1:2} but \Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() takes bool|float|int|string (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_scalar.php:32 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_scalar.php:48: Argument 1 ($value) is new \stdClass() of type \stdClass but \Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() takes bool|float|int|string (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_scalar.php:32 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_scalar.php:49: Argument 1 ($value) is null of type null but \Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() takes bool|float|int|string (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_scalar.php:32 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
|
| phpstan | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced reported Lv.5+ | phpdoc_advanced_fallback_scalar.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, array<int, int> given. [identifier=argument.type] [reported-from-level=5] phpdoc_advanced_fallback_scalar.php:48: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, stdClass given. [identifier=argument.type] [reported-from-level=5] phpdoc_advanced_fallback_scalar.php:49: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, null given. [identifier=argument.type] [reported-from-level=5] With strict-rulesphpdoc_advanced_fallback_scalar.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, array<int, int> given. [identifier=argument.type] phpdoc_advanced_fallback_scalar.php:48: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, stdClass given. [identifier=argument.type] phpdoc_advanced_fallback_scalar.php:49: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, null given. [identifier=argument.type]
|
| psalm | 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac next: 7.0.0-beta19 |
Enforced | phpdoc_advanced_fallback_scalar.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects scalar, but list{1, 2} provided [InvalidArgument]
phpdoc_advanced_fallback_scalar.php:48: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects scalar, but stdClass provided [InvalidArgument]
phpdoc_advanced_fallback_scalar.php:49: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar cannot be null, null value provided to parameter with type scalar [NullArgument]
|
| mago | 1.46.0mago 1.46.0 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar`: expected `scalar`, but found `list{int(1), int(2)}`. [invalid-argument]
phpdoc_advanced_fallback_scalar.php:48: Invalid argument type for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar`: expected `scalar`, but found `stdClass`. [invalid-argument]
phpdoc_advanced_fallback_scalar.php:49: Argument #1 of function `Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar` is `null`, but parameter type `scalar` does not accept it. [null-argument]
|
| mir | 0.70.1mir 0.70.1 | Enforced | phpdoc_advanced_fallback_scalar.php:47: InvalidArgument: Argument $value of acceptsScalar() expects 'scalar', got 'array{0: 1, 1: 2}' [MIR0201]
phpdoc_advanced_fallback_scalar.php:48: InvalidArgument: Argument $value of acceptsScalar() expects 'scalar', got 'stdClass' [MIR0201]
phpdoc_advanced_fallback_scalar.php:49: NullArgument: Argument $value of acceptsScalar() cannot be null [MIR0100]
|
| phpantom | 0.9.0phpantom_lsp 0.9.0 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Argument 1 ($value) expects scalar, got list<int> [type_mismatch_argument] phpdoc_advanced_fallback_scalar.php:48: Argument 1 ($value) expects scalar, got stdClass [type_mismatch_argument] phpdoc_advanced_fallback_scalar.php:49: Argument 1 ($value) expects scalar, got null [type_mismatch_argument]
|
| intelephense | 1.18.5intelephense 1.18.5 | Incidental (0/3) | phpdoc_advanced_fallback_scalar.php:26: Expected type 'string|int'. Found 'int|string|array'. [P1006]
Expectation diffLine 26: Expected valid value to be accepted, got ["Expected type 'string|int'. Found 'int|string|array'. [P1006]"] |
| phpy | 0.2.0phpy 0.2.0 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Argument '1' passed to Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() is expected to be of type bool|float|int|string, array given phpdoc_advanced_fallback_scalar.php:48: Argument '1' passed to Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() is expected to be of type bool|float|int|string, stdClass given phpdoc_advanced_fallback_scalar.php:49: Argument '1' passed to Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar() is expected to be of type bool|float|int|string, null given
|
| qodana | 262.8665.325Qodana 262.8665.325 | Partly enforced (2/3) | phpdoc_advanced_fallback_scalar.php:47: Parameter '[1, 2]' type is not compatible with declaration [PhpParamsInspection] phpdoc_advanced_fallback_scalar.php:48: Parameter 'new \stdClass()' type is not compatible with declaration [PhpParamsInspection]
|
| noverify | 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 | Unrecognized ⚠ 1 false positive | phpdoc_advanced_fallback_scalar.php:12: Class or interface named \Conformance\Tests\PhpdocAdvancedFallbackScalar\scalar does not exist [undefinedClass] phpdoc_advanced_fallback_scalar.php:30: Class or interface named \Conformance\Tests\PhpdocAdvancedFallbackScalar\scalar does not exist [undefinedClass] phpdoc_advanced_fallback_scalar.php:44: potentially not safe access in parameter value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar [notSafeCall] phpdoc_advanced_fallback_scalar.php:49: null passed to non-nullable parameter value in function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar [notNullSafetyFunctionArgumentConstFetch]
Expectation diffLine 44: Expected valid value to be accepted, got ["potentially not safe access in parameter value of function Conformance\\Tests\\PhpdocAdvancedFallbackScalar\\acceptsScalar [notSafeCall]"] Line 12: Unexpected errors ["Class or interface named \\Conformance\\Tests\\PhpdocAdvancedFallbackScalar\\scalar does not exist [undefinedClass]"] |
| steins | 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) | Enforced | phpdoc_advanced_fallback_scalar.php:47: argument [1, 2] to acceptsScalar() violates declared @param scalar $value — declared contract violation [phpdoc.param-mismatch] phpdoc_advanced_fallback_scalar.php:48: argument new stdClass() to acceptsScalar() violates declared @param scalar $value — declared contract violation [phpdoc.param-mismatch] phpdoc_advanced_fallback_scalar.php:49: argument null to acceptsScalar() violates declared @param scalar $value — declared contract violation [phpdoc.param-mismatch]
|
| phpstan-strict | 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, array<int, int> given. [identifier=argument.type] phpdoc_advanced_fallback_scalar.php:48: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, stdClass given. [identifier=argument.type] phpdoc_advanced_fallback_scalar.php:49: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects bool|float|int|string, null given. [identifier=argument.type]
|
| psalm-next | 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 | Enforced | phpdoc_advanced_fallback_scalar.php:47: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects scalar, but list{1, 2} provided [InvalidArgument]
phpdoc_advanced_fallback_scalar.php:48: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar expects scalar, but stdClass provided [InvalidArgument]
phpdoc_advanced_fallback_scalar.php:49: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackScalar\acceptsScalar cannot be null, null value provided to parameter with type scalar [NullArgument]
|
<?php
declare(strict_types=1);
namespace Conformance\Tests\PhpdocAdvancedFallbackScalar;
/**
* `scalar`
*
* `int|float|string|bool` — the four types that are not arrays, objects, null
* or resources. PHPStan reads the bare word as a class first and only falls
* back to the union when no such class is in scope, so the spelling is one of
* the few whose meaning depends on what else the file declares.
*
* References:
* - PHPStan TypeNodeResolver `scalar` resolves to int|float|string|bool, after trying a pseudo-type class
*/
/**
* @return scalar
*/
function returnsScalar() // T: scalar
{
$values = [1, 1.5, 'x', true];
return $values[\array_rand($values)]; // V
}
/**
* @param scalar $value
*/
function acceptsScalar($value): void // T: scalar
{
}
// The spelling round-trips: what `@return scalar` produces is what `@param
// scalar` accepts.
acceptsScalar(returnsScalar()); // V
// All four members satisfy the parameter.
acceptsScalar(1); // V
acceptsScalar(1.5); // V
acceptsScalar('x'); // V
acceptsScalar(true); // V
// Everything else is outside the union.
acceptsScalar([1, 2]); // E?: an array is not a scalar
acceptsScalar(new \stdClass()); // E?: an object is not a scalar
acceptsScalar(null); // E?: null is not a scalar