← All results

literal-string

Group: Advanced PHPDoc types · Category: ecosystem · File: phpdoc_advanced_fallback_literal_string.php

A string whose every character comes from source code rather than from runtime input — the refinement injection-sensitive APIs ask for. Concatenating two literals keeps it literal; mixing in a value of unknown provenance does not. Analyzers that model it reject a runtime string; others fall back to plain string and accept it. References: - PHPStan TypeNodeResolver literal-string resolves to string&AccessoryLiteralStringType

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Unrecognized
phpdoc_advanced_fallback_literal_string.php:21: Saw a token Phan may have failed to parse after '* @return literal-string': after literal, saw '-' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_fallback_literal_string.php:23: Return type of returnsLiteralString() is undeclared type \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal [PhanUndeclaredTypeReturnType]
phpdoc_advanced_fallback_literal_string.php:25: Returning 'literal' of type string but returnsLiteralString() is declared to return \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) (the inferred real return type has nothing in common with the declared phpdoc return type) [PhanTypeMismatchReturnProbablyReal]
phpdoc_advanced_fallback_literal_string.php:33: Saw possibly unextractable annotation for a fragment of comment '* @param literal-string $value': after literal, did not see an element name (will guess based on comment order) [PhanUnextractableAnnotationElementName]
phpdoc_advanced_fallback_literal_string.php:33: Saw a token Phan may have failed to parse after '* @param literal-string $value': after literal, saw '-' [PhanUnextractableAnnotationSuffix]
phpdoc_advanced_fallback_literal_string.php:35: Parameter $value has undeclared type \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal [PhanUndeclaredTypeParameter]
phpdoc_advanced_fallback_literal_string.php:40: Argument 1 ($value) is returnsLiteralString() of type \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsString() takes string defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:28 [PhanTypeMismatchArgument]
phpdoc_advanced_fallback_literal_string.php:44: Argument 1 ($value) is 'a literal' of type string but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString() takes \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_literal_string.php:45: Argument 1 ($value) is 'a literal' of type string but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString() takes \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_literal_string.php:46: Argument 1 ($value) is '' of type string but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString() takes \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_literal_string.php:52: Argument 1 ($value) is $value of type string but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString() takes \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
phpdoc_advanced_fallback_literal_string.php:55: Argument 1 ($value) is ('prefix ' . $value) of type string but \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString() takes \Conformance\Tests\PhpdocAdvancedFallbackLiteralString\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
  • Recognition: spelling not resolved — reported on declaration line(s) 21, 23, 33, 35
  • Enforcement: 2/2 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 25: Expected valid value to be accepted, got ["Returning 'literal' of type string but returnsLiteralString() is declared to return \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\literal (no real type) (the inferred real return type has nothing in common with the declared phpdoc return type) [PhanTypeMismatchReturnProbablyReal]"]
Line 40: Expected valid value to be accepted, got ["Argument 1 ($value) is returnsLiteralString() of type \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\literal but \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\acceptsString() takes string defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:28 [PhanTypeMismatchArgument]"]
Line 44: Expected valid value to be accepted, got ["Argument 1 ($value) is 'a literal' of type string but \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\acceptsLiteralString() takes \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]"]
Line 45: Expected valid value to be accepted, got ["Argument 1 ($value) is 'a literal' of type string but \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\acceptsLiteralString() takes \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]"]
Line 46: Expected valid value to be accepted, got ["Argument 1 ($value) is '' of type string but \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\acceptsLiteralString() takes \\Conformance\\Tests\\PhpdocAdvancedFallbackLiteralString\\literal (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/phpdoc_advanced_fallback_literal_string.php:35 (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.7+
phpdoc_advanced_fallback_literal_string.php:52: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, string given. [identifier=argument.type] [reported-from-level=7]
phpdoc_advanced_fallback_literal_string.php:55: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, non-falsy-string given. [identifier=argument.type] [reported-from-level=7]
With strict-rules
phpdoc_advanced_fallback_literal_string.php:52: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, string given. [identifier=argument.type]
phpdoc_advanced_fallback_literal_string.php:55: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, non-falsy-string given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Enforced
phpdoc_advanced_fallback_literal_string.php:52: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, but parent type string provided [ArgumentTypeCoercion]
phpdoc_advanced_fallback_literal_string.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, but parent type non-falsy-string provided [ArgumentTypeCoercion]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mago 1.46.0mago 1.46.0 Enforced
phpdoc_advanced_fallback_literal_string.php:52: Possible argument type mismatch for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString`: expected `literal-string`, but possibly received `string`. [possibly-invalid-argument]
phpdoc_advanced_fallback_literal_string.php:55: Possible argument type mismatch for argument #1 of `Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString`: expected `literal-string`, but possibly received `truthy-string`. [possibly-invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mir 0.70.1mir 0.70.1 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported

Notes: Was measured 'Enforced 2/2' under mir 0.62.0, but that was a parsing artifact, not real enforcement: mir's changelog for 0.63.0 says literal-int/literal-string previously 'fell through to a bogus named-class bound, always failing a genuinely-satisfying literal argument' — it rejected every literal-string argument, valid or not, which is also why false positives showed up on lines 40/44/45/46. 0.63.0 fixed the recognition, and the over-rejection (both the coincidental hits on these probes and the false positives) went away together, leaving the honest state: recognized, not yet enforced.

phpantom 0.9.0phpantom_lsp 0.9.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Widened to string

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpy 0.2.0phpy 0.2.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.8665.325Qodana 262.8665.325 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Widened to string

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported

Notes: `literal-string` lowers to `ContractTy::StrOpaque` (ADR-0038): provenance is not an extensional value property, so Steins can never confirm a string came from source code — every string is `Maybe`, exactly as a bare `string` parameter, and only non-strings are rejected.

phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Enforced
phpdoc_advanced_fallback_literal_string.php:52: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, string given. [identifier=argument.type]
phpdoc_advanced_fallback_literal_string.php:55: Parameter #1 $value of function Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, non-falsy-string given. [identifier=argument.type]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Enforced
phpdoc_advanced_fallback_literal_string.php:52: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, but parent type string provided [ArgumentTypeCoercion]
phpdoc_advanced_fallback_literal_string.php:55: Argument 1 of Conformance\Tests\PhpdocAdvancedFallbackLiteralString\acceptsLiteralString expects literal-string, but parent type non-falsy-string provided [ArgumentTypeCoercion]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\PhpdocAdvancedFallbackLiteralString;

/**
 * `literal-string`
 *
 * A string whose every character comes from source code rather than from
 * runtime input — the refinement injection-sensitive APIs ask for. Concatenating
 * two literals keeps it literal; mixing in a value of unknown provenance does
 * not. Analyzers that model it reject a runtime string; others fall back to
 * plain `string` and accept it.
 *
 * References:
 * - PHPStan TypeNodeResolver `literal-string` resolves to string&AccessoryLiteralStringType
 */

/**
 * @return literal-string
 */
function returnsLiteralString() // T: literal-string
{
    return 'literal'; // V
}

function acceptsString(string $value): void
{
}

/**
 * @param literal-string $value
 */
function acceptsLiteralString($value): void // T: literal-string
{
}

// A `literal-string` value always satisfies a native `string` parameter.
acceptsString(returnsLiteralString()); // V

// Source-code literals satisfy the parameter, including a concatenation of two
// of them and the empty string.
acceptsLiteralString('a literal'); // V
acceptsLiteralString('a' . ' literal'); // V
acceptsLiteralString(''); // V

function forwardsRuntimeString(string $value): void
{
    // A string of unknown provenance: enforcing analyzers reject it, others
    // fall back to `string` and accept it.
    acceptsLiteralString($value); // E?: an arbitrary runtime string is not a literal-string

    // Concatenating a literal onto it does not launder it either.
    acceptsLiteralString('prefix ' . $value); // E?: a literal concatenated with a runtime string is not a literal-string
}