Issues (72)

cli/make.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
use Cerbero\Enum\Enums\Backed;
6
use Cerbero\Enum\Services\Generator;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Generator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
use function Cerbero\Enum\enumOutcome;
9
use function Cerbero\Enum\fail;
10
use function Cerbero\Enum\option;
11
use function Cerbero\Enum\runAnnotate;
12
use function Cerbero\Enum\runTs;
13
use function Cerbero\Enum\succeed;
14
15
if (! $enum = strtr($arguments[0] ?? '', '/', '\\')) {
16
    return fail('The name of the enum is missing.');
17
}
18
19
$force = !! array_intersect(['--force', '-f'], $options);
20
21
if (enum_exists($enum) && ! $force) {
0 ignored issues
show
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
if (/** @scrutinizer ignore-call */ enum_exists($enum) && ! $force) {
Loading history...
22
    return succeed("The enum {$enum} already exists.");
23
}
24
25
if (! $cases = array_slice($arguments, 1)) {
26
    return fail('The cases of the enum are missing.');
27
}
28
29
try {
30
    $generator = new Generator($enum, $cases, option('backed', $options));
31
} catch (ValueError) {
32
    return fail('The option --backed supports only ' . implode(', ', Backed::names()));
33
}
34
35
$typeScript = !! array_intersect(['--typescript', '-t'], $options);
36
37
return enumOutcome($enum, function () use ($generator, $enum, $force, $typeScript) {
38
    return $generator->generate($force)
39
        && runAnnotate($enum, $force)
40
        && ($typeScript ? runTs($enum, $force) : true);
41
});
42