From e61edefbab2b2d86c6d8ff8fd37037bf4212a3ce Mon Sep 17 00:00:00 2001 From: Mikhail Alferov Date: Mon, 18 Nov 2024 00:37:11 +0300 Subject: [PATCH] enumerations.xml Amend the CS of the code examples --- language/enumerations.xml | 63 +++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/language/enumerations.xml b/language/enumerations.xml index eab8c015bf6a..7d46788747f6 100644 --- a/language/enumerations.xml +++ b/language/enumerations.xml @@ -45,6 +45,7 @@ enum Suit case Clubs; case Spades; } + ?> ]]> @@ -76,6 +77,7 @@ pick_a_card(Suit::Clubs); // TypeError: pick_a_card(): Argument #1 ($suit) must be of type Suit, string given pick_a_card('Spades'); + ?> ]]> @@ -105,6 +107,7 @@ $b = Suit::Spades; $a === $b; // true $a instanceof Suit; // true + ?> ]]> @@ -135,6 +138,7 @@ $a instanceof Suit; // true print Suit::Spades->name; // prints "Spades" + ?> ]]> @@ -171,6 +175,7 @@ enum Suit: string case Clubs = 'C'; case Spades = 'S'; } + ?> ]]> @@ -208,6 +213,7 @@ enum Suit: string print Suit::Clubs->value; // Prints "C" + ?> ]]> @@ -224,6 +230,7 @@ print Suit::Clubs->value; $suit = Suit::Clubs; $ref = &$suit->value; // Error: Cannot acquire reference to property Suit::$value + ?> ]]> @@ -272,6 +279,7 @@ print $suit->value; $suit = Suit::tryFrom('A') ?? Suit::Spades; // Invalid data returns null, so Suit::Spades is used instead. print $suit->value; + ?> ]]> @@ -308,7 +316,7 @@ enum Suit implements Colorful // Fulfills the interface contract. public function color(): string { - return match($this) { + return match ($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; @@ -329,6 +337,7 @@ function paint(Colorful $c) paint(Suit::Clubs); // Works print Suit::Diamonds->shape(); // prints "Rectangle" + ?> ]]> @@ -362,12 +371,13 @@ enum Suit: string implements Colorful // Fulfills the interface contract. public function color(): string { - return match($this) { + return match ($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; } } + ?> ]]> @@ -413,7 +423,7 @@ final class Suit implements UnitEnum, Colorful public function color(): string { - return match($this) { + return match ($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; @@ -430,6 +440,7 @@ final class Suit implements UnitEnum, Colorful // See also "Value listing" section. } } + ?> ]]> @@ -461,13 +472,14 @@ enum Size public static function fromLength(int $cm): static { - return match(true) { + return match (true) { $cm < 50 => static::Small, $cm < 100 => static::Medium, default => static::Large, }; } } + ?> ]]> @@ -501,6 +513,7 @@ enum Size public const Huge = self::Large; } + ?> ]]> @@ -526,7 +539,8 @@ interface Colorful trait Rectangle { - public function shape(): string { + public function shape(): string + { return "Rectangle"; } } @@ -542,12 +556,13 @@ enum Suit implements Colorful public function color(): string { - return match($this) { + return match ($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; } } + ?> ]]> @@ -616,6 +631,7 @@ $x = Direction::Up['short']; var_dump("\$x is " . var_export($x, true)); $foo = new Foo(); + ?> ]]> @@ -679,6 +695,7 @@ $clovers = new Suit(); $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor() // Error: Cannot instantiate enum Suit + ?> ]]> @@ -700,6 +717,7 @@ $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor( Suit::cases(); // Produces: [Suit::Hearts, Suit::Diamonds, Suit::Clubs, Suit::Spades] + ?> ]]> @@ -724,6 +742,7 @@ Suit::Hearts === unserialize(serialize(Suit::Hearts)); print serialize(Suit::Hearts); // E:11:"Suit:Hearts"; + ?> ]]> @@ -747,11 +766,13 @@ print serialize(Suit::Hearts); ]]> @@ -811,7 +834,8 @@ function bar(B $b) { true, - } + }; } ?> @@ -843,11 +867,13 @@ function quux(ErrorCode $errorCode) // Thought experiment code where enums are not final. // Note, this won't actually work in PHP. -enum MoreErrorCode extends ErrorCode { +enum MoreErrorCode extends ErrorCode +{ case PEBKAC; } -function fot(MoreErrorCode $errorCode) { +function fot(MoreErrorCode $errorCode) +{ quux($errorCode); } @@ -892,6 +918,7 @@ function query($fields, $filter, SortOrder $order = SortOrder::Asc) { /* ... */ } + ?> ]]> @@ -922,7 +949,7 @@ enum UserStatus: string public function label(): string { - return match($this) { + return match ($this) { static::Pending => 'Pending', static::Active => 'Active', static::Suspended => 'Suspended', @@ -930,6 +957,7 @@ enum UserStatus: string }; } } + ?> ]]> @@ -952,8 +980,13 @@ enum UserStatus: string %s\n', $case->value, $case->label()); + printf( + "\n", + $case->value, + $case->label() + ); } + ?> ]]>