Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enumerations.xml Amend the CS of the code examples #4088

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 48 additions & 15 deletions language/enumerations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum Suit
case Clubs;
case Spades;
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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');

?>
]]>
</programlisting>
Expand Down Expand Up @@ -105,6 +107,7 @@ $b = Suit::Spades;
$a === $b; // true

$a instanceof Suit; // true

?>
]]>
</programlisting>
Expand Down Expand Up @@ -135,6 +138,7 @@ $a instanceof Suit; // true

print Suit::Spades->name;
// prints "Spades"

?>
]]>
</programlisting>
Expand Down Expand Up @@ -171,6 +175,7 @@ enum Suit: string
case Clubs = 'C';
case Spades = 'S';
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -208,6 +213,7 @@ enum Suit: string

print Suit::Clubs->value;
// Prints "C"

?>
]]>
</programlisting>
Expand All @@ -224,6 +230,7 @@ print Suit::Clubs->value;
$suit = Suit::Clubs;
$ref = &$suit->value;
// Error: Cannot acquire reference to property Suit::$value

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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;

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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',
};
Expand All @@ -329,6 +337,7 @@ function paint(Colorful $c)
paint(Suit::Clubs); // Works

print Suit::Diamonds->shape(); // prints "Rectangle"

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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',
};
}
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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',
};
Expand All @@ -430,6 +440,7 @@ final class Suit implements UnitEnum, Colorful
// See also "Value listing" section.
}
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -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,
};
}
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -501,6 +513,7 @@ enum Size

public const Huge = self::Large;
}

?>
]]>
</programlisting>
Expand All @@ -526,7 +539,8 @@ interface Colorful

trait Rectangle
{
public function shape(): string {
public function shape(): string
{
return "Rectangle";
}
}
Expand All @@ -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',
};
}
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -616,6 +631,7 @@ $x = Direction::Up['short'];
var_dump("\$x is " . var_export($x, true));

$foo = new Foo();

?>
]]>
</programlisting>
Expand Down Expand Up @@ -679,6 +695,7 @@ $clovers = new Suit();

$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
// Error: Cannot instantiate enum Suit

?>
]]>
</programlisting>
Expand All @@ -700,6 +717,7 @@ $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor(

Suit::cases();
// Produces: [Suit::Hearts, Suit::Diamonds, Suit::Clubs, Suit::Spades]

?>
]]>
</programlisting>
Expand All @@ -724,6 +742,7 @@ Suit::Hearts === unserialize(serialize(Suit::Hearts));

print serialize(Suit::Hearts);
// E:11:"Suit:Hearts";

?>
]]>
</programlisting>
Expand All @@ -747,11 +766,13 @@ print serialize(Suit::Hearts);
<![CDATA[
<?php

enum Foo {
enum Foo
{
case Bar;
}

enum Baz: int {
enum Baz: int
{
case Beep = 5;
}

Expand Down Expand Up @@ -790,9 +811,11 @@ class B extends A {}

function foo(A $a) {}

function bar(B $b) {
function bar(B $b)
{
foo($b);
}

?>
]]>
</programlisting>
Expand All @@ -811,7 +834,8 @@ function bar(B $b) {
<![CDATA[
<?php

enum ErrorCode {
enum ErrorCode
{
case SOMETHING_BROKE;
}

Expand All @@ -820,7 +844,7 @@ function quux(ErrorCode $errorCode)
// When written, this code appears to cover all cases
match ($errorCode) {
ErrorCode::SOMETHING_BROKE => true,
}
};
}

?>
Expand All @@ -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);
}

Expand Down Expand Up @@ -892,6 +918,7 @@ function query($fields, $filter, SortOrder $order = SortOrder::Asc)
{
/* ... */
}

?>
]]>
</programlisting>
Expand Down Expand Up @@ -922,14 +949,15 @@ enum UserStatus: string

public function label(): string
{
return match($this) {
return match ($this) {
static::Pending => 'Pending',
static::Active => 'Active',
static::Suspended => 'Suspended',
static::CanceledByUser => 'Canceled by user',
};
}
}

?>
]]>
</programlisting>
Expand All @@ -952,8 +980,13 @@ enum UserStatus: string
<?php

foreach (UserStatus::cases() as $case) {
printf('<option value="%s">%s</option>\n', $case->value, $case->label());
printf(
"<option value=\"%s\">%s</option>\n",
$case->value,
$case->label()
);
}

?>
]]>
</programlisting>
Expand Down