Skip to content

Commit a7e59bb

Browse files
authored
Add enum support (#725)
1 parent 1a30d98 commit a7e59bb

File tree

4 files changed

+335
-0
lines changed

4 files changed

+335
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
6666
- [Composer Plugins](docs/limitations.md#composer-plugins)
6767
- [PSR-0 Partial support](docs/limitations.md#psr-0-partial-support)
6868
- [Files autoloading](docs/limitations.md#files-autoloading)
69+
- [Exposing/Excluding traits](docs/limitations.md#exposingexcluding-traits)
70+
- [Exposing/Excluding enums](docs/limitations.md#exposingexcluding-enums)
6971
- [Contributing](#contributing)
7072
- [Credits](#credits)
7173

docs/limitations.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- [Composer Plugins](#composer-plugins)
1111
- [PSR-0 Partial support](#psr-0-partial-support)
1212
- [Files autoloading](#files-autoloading)
13+
- [Exposing/Excluding traits](#exposingexcluding-traits)
14+
- [Exposing/Excluding enums](#exposingexcluding-enums)
1315

1416

1517
### Dynamic symbols
@@ -240,6 +242,19 @@ the scoped file and non-scoped file will have the same hash resulting in errors.
240242
This is a limitation that should be fixable, check [#298] for the progress.
241243

242244

245+
### Exposing/Excluding traits
246+
247+
There is currently no way to expose or exclude a trait. Since there is no
248+
aliasing mechanism for traits, it could be still possible by declaring a trait
249+
that extends the scoped trait, but this is currently not implemented.
250+
251+
252+
### Exposing/Excluding enums
253+
254+
There is currently no way to expose or exclude an enum. The problem being there
255+
is no way to alias one.
256+
257+
243258
<br />
244259
<hr />
245260

specs/enum/declaration.php

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Enum declaration',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
21+
'expose-global-constants' => false,
22+
'expose-global-classes' => false,
23+
'expose-global-functions' => false,
24+
'expose-namespaces' => [],
25+
'expose-constants' => [],
26+
'expose-classes' => [],
27+
'expose-functions' => [],
28+
29+
'exclude-namespaces' => [],
30+
'exclude-constants' => [],
31+
'exclude-classes' => [],
32+
'exclude-functions' => [],
33+
34+
'expected-recorded-classes' => [],
35+
'expected-recorded-functions' => [],
36+
],
37+
38+
'minimal enum declaration' => <<<'PHP'
39+
<?php
40+
41+
enum Status {
42+
case DRAFT;
43+
case PUBLISHED;
44+
case ARCHIVED;
45+
}
46+
47+
----
48+
<?php
49+
50+
namespace Humbug;
51+
52+
enum Status
53+
{
54+
case DRAFT;
55+
case PUBLISHED;
56+
case ARCHIVED;
57+
}
58+
59+
PHP,
60+
61+
'enum with methods' => <<<'PHP'
62+
<?php
63+
64+
enum Status {
65+
case DRAFT;
66+
case PUBLISHED;
67+
case ARCHIVED;
68+
69+
public function color(): string {
70+
return match($this) {
71+
Status::DRAFT => 'grey',
72+
Status::PUBLISHED => 'green',
73+
self::ARCHIVED => 'red',
74+
};
75+
}
76+
}
77+
78+
----
79+
<?php
80+
81+
namespace Humbug;
82+
83+
enum Status
84+
{
85+
case DRAFT;
86+
case PUBLISHED;
87+
case ARCHIVED;
88+
public function color() : string
89+
{
90+
return match ($this) {
91+
Status::DRAFT => 'grey',
92+
Status::PUBLISHED => 'green',
93+
self::ARCHIVED => 'red',
94+
};
95+
}
96+
}
97+
98+
PHP,
99+
100+
'enum with interface' => <<<'PHP'
101+
<?php
102+
103+
enum Status implements HasColor {
104+
case DRAFT = 'draft';
105+
case PUBLISHED = 'published';
106+
case ARCHIVED = 'archived';
107+
}
108+
109+
----
110+
<?php
111+
112+
namespace Humbug;
113+
114+
enum Status implements \HasColor
115+
{
116+
case DRAFT = 'draft';
117+
case PUBLISHED = 'published';
118+
case ARCHIVED = 'archived';
119+
}
120+
121+
PHP,
122+
123+
'class with Enum name' => <<<'PHP'
124+
<?php
125+
126+
class Enum {}
127+
128+
----
129+
<?php
130+
131+
namespace Humbug;
132+
133+
class Enum
134+
{
135+
}
136+
137+
PHP,
138+
139+
'backed enum' => <<<'PHP'
140+
<?php
141+
142+
enum Status: string {
143+
case DRAFT = 'draft';
144+
case PUBLISHED = 'published';
145+
case ARCHIVED = 'archived';
146+
}
147+
148+
----
149+
<?php
150+
151+
namespace Humbug;
152+
153+
enum Status : string
154+
{
155+
case DRAFT = 'draft';
156+
case PUBLISHED = 'published';
157+
case ARCHIVED = 'archived';
158+
}
159+
160+
PHP,
161+
162+
'excluded enum (doesn\'t work)' => [
163+
'exclude-classes' => ['Status'],
164+
'payload' => <<<'PHP'
165+
<?php
166+
167+
enum Status {
168+
case DRAFT;
169+
case PUBLISHED;
170+
case ARCHIVED;
171+
}
172+
173+
----
174+
<?php
175+
176+
namespace Humbug;
177+
178+
enum Status
179+
{
180+
case DRAFT;
181+
case PUBLISHED;
182+
case ARCHIVED;
183+
}
184+
185+
PHP
186+
],
187+
188+
'exposed enum (doesn\'t work)' => [
189+
'expose-classes' => ['Status'],
190+
'payload' => <<<'PHP'
191+
<?php
192+
193+
enum Status {
194+
case DRAFT;
195+
case PUBLISHED;
196+
case ARCHIVED;
197+
}
198+
199+
----
200+
<?php
201+
202+
namespace Humbug;
203+
204+
enum Status
205+
{
206+
case DRAFT;
207+
case PUBLISHED;
208+
case ARCHIVED;
209+
}
210+
211+
PHP
212+
],
213+
];

specs/enum/usage.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Enum declaration',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
21+
'expose-global-constants' => false,
22+
'expose-global-classes' => false,
23+
'expose-global-functions' => false,
24+
'expose-namespaces' => [],
25+
'expose-constants' => [],
26+
'expose-classes' => [],
27+
'expose-functions' => [],
28+
29+
'exclude-namespaces' => [],
30+
'exclude-constants' => [],
31+
'exclude-classes' => [],
32+
'exclude-functions' => [],
33+
34+
'expected-recorded-classes' => [],
35+
'expected-recorded-functions' => [],
36+
],
37+
38+
'typehint and create an enum' => <<<'PHP'
39+
<?php
40+
41+
namespace Acme;
42+
43+
use Status;
44+
45+
class BlogPost
46+
{
47+
public function __construct(
48+
public Status $status,
49+
) {}
50+
}
51+
$post = new BlogPost(Status::DRAFT);
52+
53+
----
54+
<?php
55+
56+
namespace Humbug\Acme;
57+
58+
use Humbug\Status;
59+
class BlogPost
60+
{
61+
public function __construct(public Status $status)
62+
{
63+
}
64+
}
65+
$post = new BlogPost(Status::DRAFT);
66+
67+
PHP,
68+
69+
'use an enum method' => <<<'PHP'
70+
<?php
71+
72+
namespace Acme;
73+
74+
use Status;
75+
76+
$status = Status::ARCHIVED;
77+
$status->color();
78+
79+
----
80+
<?php
81+
82+
namespace Humbug\Acme;
83+
84+
use Humbug\Status;
85+
$status = Status::ARCHIVED;
86+
$status->color();
87+
88+
PHP,
89+
90+
'use instance of enum' => <<<'PHP'
91+
<?php
92+
93+
namespace Acme;
94+
95+
$statusC instanceof \Post\Status;
96+
97+
----
98+
<?php
99+
100+
namespace Humbug\Acme;
101+
102+
$statusC instanceof \Humbug\Post\Status;
103+
104+
PHP,
105+
];

0 commit comments

Comments
 (0)