1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<?php
class Foo
{
/* testReadonlyProperty */
readonly int $readonlyProperty;
/* testVarReadonlyProperty */
var readonly int $varReadonlyProperty;
/* testReadonlyVarProperty */
readonly var int $testReadonlyVarProperty;
/* testStaticReadonlyProperty */
static readonly int $staticReadonlyProperty;
/* testReadonlyStaticProperty */
readonly static int $readonlyStaticProperty;
/* testConstReadonlyProperty */
public readonly const MYCONSTANT = 'foo';
/* testReadonlyPropertyWithoutType */
readonly $propertyWithoutType;
/* testPublicReadonlyProperty */
public readonly int $publicReadonlyProperty;
/* testProtectedReadonlyProperty */
protected readonly int $protectedReadonlyProperty;
/* testPrivateReadonlyProperty */
private readonly int $privateReadonlyProperty;
/* testPublicReadonlyPropertyWithReadonlyFirst */
readonly public int $publicReadonlyProperty;
/* testProtectedReadonlyPropertyWithReadonlyFirst */
readonly protected int $protectedReadonlyProperty;
/* testPrivateReadonlyPropertyWithReadonlyFirst */
readonly private int $privateReadonlyProperty;
/* testReadonlyWithCommentsInDeclaration */
private /* Comment */ readonly /* Comment */ int /* Comment */ $readonlyPropertyWithCommentsInDeclaration;
/* testReadonlyWithNullableProperty */
private readonly ?int $nullableProperty;
/* testReadonlyNullablePropertyWithUnionTypeHintAndNullFirst */
private readonly null|int $nullablePropertyWithUnionTypeHintAndNullFirst;
/* testReadonlyNullablePropertyWithUnionTypeHintAndNullLast */
private readonly int|null $nullablePropertyWithUnionTypeHintAndNullLast;
/* testReadonlyPropertyWithArrayTypeHint */
private readonly array $arrayProperty;
/* testReadonlyPropertyWithSelfTypeHint */
private readonly self $selfProperty;
/* testReadonlyPropertyWithParentTypeHint */
private readonly parent $parentProperty;
/* testReadonlyPropertyWithFullyQualifiedTypeHint */
private readonly \stdClass $propertyWithFullyQualifiedTypeHint;
/* testReadonlyIsCaseInsensitive */
public ReAdOnLy string $caseInsensitiveProperty;
/* testReadonlyConstructorPropertyPromotion */
public function __construct(private readonly bool $constructorPropertyPromotion) {}
/* testReadonlyConstructorPropertyPromotionWithReference */
public function __construct(private ReadOnly bool &$constructorPropertyPromotion) {}
}
$anonymousClass = new class () {
/* testReadonlyPropertyInAnonymousClass */
public readonly int $property;
};
class ClassName {
/* testReadonlyUsedAsClassConstantName */
const READONLY = 'readonly';
/* testReadonlyUsedAsMethodName */
public function readonly() {
/* testReadonlyUsedAsPropertyName */
$this->readonly = 'foo';
/* testReadonlyPropertyInTernaryOperator */
$isReadonly = $this->readonly ? true : false;
}
}
/* testReadonlyUsedAsFunctionName */
function readonly() {}
/* testReadonlyUsedAsFunctionNameWithReturnByRef */
function &readonly() {}
/* testReadonlyUsedAsNamespaceName */
namespace Readonly;
/* testReadonlyUsedAsPartOfNamespaceName */
namespace My\Readonly\Collection;
/* testReadonlyAsFunctionCall */
$var = readonly($a, $b);
/* testReadonlyAsNamespacedFunctionCall */
$var = My\NS\readonly($a, $b);
/* testReadonlyAsNamespaceRelativeFunctionCall */
$var = namespace\ReadOnly($a, $b);
/* testReadonlyAsMethodCall */
$var = $obj->readonly($a, $b);
/* testReadonlyAsNullsafeMethodCall */
$var = $obj?->readOnly($a, $b);
/* testReadonlyAsStaticMethodCallWithSpace */
$var = ClassName::readonly ($a, $b);
/* testClassConstantFetchWithReadonlyAsConstantName */
echo ClassName::READONLY;
/* testReadonlyUsedAsFunctionCallWithSpaceBetweenKeywordAndParens */
$var = readonly /* comment */ ();
// These test cases are inspired by
// https://github.com/php/php-src/commit/08b75395838b4b42a41e3c70684fa6c6b113eee0
class ReadonlyWithDisjunctiveNormalForm
{
/* testReadonlyPropertyDNFTypeUnqualified */
readonly (B&C)|A $h;
/* testReadonlyPropertyDNFTypeFullyQualified */
public readonly (\Fully\Qualified\B&\Full\C)|\Foo\Bar $j;
/* testReadonlyPropertyDNFTypePartiallyQualified */
protected readonly (Partially\Qualified&C)|A $l;
/* testReadonlyPropertyDNFTypeRelativeName */
private readonly (namespace\Relative&C)|A $n;
/* testReadonlyPropertyDNFTypeMultipleSets */
private readonly (A&C)|(B&C)|(C&D) $m;
/* testReadonlyPropertyDNFTypeWithArray */
private readonly (B & C)|array $o;
/* testReadonlyPropertyDNFTypeWithSpacesAndComments */
private readonly ( B & C /*something*/) | A $q;
public function __construct(
/* testReadonlyConstructorPropertyPromotionWithDNF */
private readonly (B&C)|A $b1,
/* testReadonlyConstructorPropertyPromotionWithDNFAndReference */
readonly (B&C)|A &$b2,
) {}
/* testReadonlyUsedAsMethodNameWithDNFParam */
public function readonly (A&B $param): void {}
}
/* testReadonlyAnonClassWithParens */
$anon = new readonly class() {};
/* testReadonlyAnonClassWithoutParens */
$anon = new Readonly class {};
/* testReadonlyAnonClassWithCommentsAndWhitespace */
$anon = new
// comment
READONLY
// phpcs:ignore Stnd.Cat.Sniff
class {};
/* testParseErrorLiveCoding */
// This must be the last test in the file.
readonly
|