Skip to content

Commit

Permalink
Add enum support (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 9, 2022
1 parent 1a30d98 commit a7e59bb
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [Composer Plugins](docs/limitations.md#composer-plugins)
- [PSR-0 Partial support](docs/limitations.md#psr-0-partial-support)
- [Files autoloading](docs/limitations.md#files-autoloading)
- [Exposing/Excluding traits](docs/limitations.md#exposingexcluding-traits)
- [Exposing/Excluding enums](docs/limitations.md#exposingexcluding-enums)
- [Contributing](#contributing)
- [Credits](#credits)

Expand Down
15 changes: 15 additions & 0 deletions docs/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [Composer Plugins](#composer-plugins)
- [PSR-0 Partial support](#psr-0-partial-support)
- [Files autoloading](#files-autoloading)
- [Exposing/Excluding traits](#exposingexcluding-traits)
- [Exposing/Excluding enums](#exposingexcluding-enums)


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


### Exposing/Excluding traits

There is currently no way to expose or exclude a trait. Since there is no
aliasing mechanism for traits, it could be still possible by declaring a trait
that extends the scoped trait, but this is currently not implemented.


### Exposing/Excluding enums

There is currently no way to expose or exclude an enum. The problem being there
is no way to alias one.


<br />
<hr />

Expand Down
213 changes: 213 additions & 0 deletions specs/enum/declaration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'meta' => [
'title' => 'Enum declaration',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',

'expose-global-constants' => false,
'expose-global-classes' => false,
'expose-global-functions' => false,
'expose-namespaces' => [],
'expose-constants' => [],
'expose-classes' => [],
'expose-functions' => [],

'exclude-namespaces' => [],
'exclude-constants' => [],
'exclude-classes' => [],
'exclude-functions' => [],

'expected-recorded-classes' => [],
'expected-recorded-functions' => [],
],

'minimal enum declaration' => <<<'PHP'
<?php
enum Status {
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}
----
<?php
namespace Humbug;
enum Status
{
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}

PHP,

'enum with methods' => <<<'PHP'
<?php
enum Status {
case DRAFT;
case PUBLISHED;
case ARCHIVED;
public function color(): string {
return match($this) {
Status::DRAFT => 'grey',
Status::PUBLISHED => 'green',
self::ARCHIVED => 'red',
};
}
}
----
<?php
namespace Humbug;
enum Status
{
case DRAFT;
case PUBLISHED;
case ARCHIVED;
public function color() : string
{
return match ($this) {
Status::DRAFT => 'grey',
Status::PUBLISHED => 'green',
self::ARCHIVED => 'red',
};
}
}

PHP,

'enum with interface' => <<<'PHP'
<?php
enum Status implements HasColor {
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
----
<?php
namespace Humbug;
enum Status implements \HasColor
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}

PHP,

'class with Enum name' => <<<'PHP'
<?php
class Enum {}
----
<?php
namespace Humbug;
class Enum
{
}

PHP,

'backed enum' => <<<'PHP'
<?php
enum Status: string {
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
----
<?php
namespace Humbug;
enum Status : string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}

PHP,

'excluded enum (doesn\'t work)' => [
'exclude-classes' => ['Status'],
'payload' => <<<'PHP'
<?php
enum Status {
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}
----
<?php
namespace Humbug;
enum Status
{
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}

PHP
],

'exposed enum (doesn\'t work)' => [
'expose-classes' => ['Status'],
'payload' => <<<'PHP'
<?php
enum Status {
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}
----
<?php
namespace Humbug;
enum Status
{
case DRAFT;
case PUBLISHED;
case ARCHIVED;
}

PHP
],
];
105 changes: 105 additions & 0 deletions specs/enum/usage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'meta' => [
'title' => 'Enum declaration',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',

'expose-global-constants' => false,
'expose-global-classes' => false,
'expose-global-functions' => false,
'expose-namespaces' => [],
'expose-constants' => [],
'expose-classes' => [],
'expose-functions' => [],

'exclude-namespaces' => [],
'exclude-constants' => [],
'exclude-classes' => [],
'exclude-functions' => [],

'expected-recorded-classes' => [],
'expected-recorded-functions' => [],
],

'typehint and create an enum' => <<<'PHP'
<?php
namespace Acme;
use Status;
class BlogPost
{
public function __construct(
public Status $status,
) {}
}
$post = new BlogPost(Status::DRAFT);
----
<?php
namespace Humbug\Acme;
use Humbug\Status;
class BlogPost
{
public function __construct(public Status $status)
{
}
}
$post = new BlogPost(Status::DRAFT);

PHP,

'use an enum method' => <<<'PHP'
<?php
namespace Acme;
use Status;
$status = Status::ARCHIVED;
$status->color();
----
<?php
namespace Humbug\Acme;
use Humbug\Status;
$status = Status::ARCHIVED;
$status->color();

PHP,

'use instance of enum' => <<<'PHP'
<?php
namespace Acme;
$statusC instanceof \Post\Status;
----
<?php
namespace Humbug\Acme;
$statusC instanceof \Humbug\Post\Status;

PHP,
];

0 comments on commit a7e59bb

Please sign in to comment.