Completed
Push — master ( 1cd2c7...bcc930 )
by Guillermo
10s
created

EnumDocHelper::main()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Atrapalo\PHPTools\Enum;
4
5
/**
6
 * Class EnumDocHelper
7
 * @package Atrapalo\PHPTools\Enum
8
 *
9
 * @author Guillermo González <[email protected]>
10
 */
11
class EnumDocHelper
12
{
13
    /**
14
     * @return int
15
     */
16 5
    public static function main(): int
17
    {
18 5
        $command = new static;
19
20 5
        return $command->run($_SERVER['argv']);
21
    }
22
23
    /**
24
     * @param array $arguments
25
     * @return int
26
     */
27 5
    public function run(array $arguments): int
28
    {
29
        try {
30 5
            $className = $this->handleArguments($arguments);
31 5
            if (class_exists($className)) {
32 4
                $reflection = new \ReflectionClass($className);
33 4
                $constants = $this->constants($reflection);
34
35 1
                $this->printHeader($reflection->getShortName(), $reflection->getNamespaceName());
36 1
                $this->printMethods($constants, $reflection->getShortName());
37 1
                $this->printFooter();
38
            } else {
39 2
                throw new \InvalidArgumentException('Class not exist');
40
            }
41 4
        } catch (\Exception $exception) {
42 4
            $this->printError($exception->getMessage());
43
        }
44
45 5
        return 0;
46
    }
47
48
    /**
49
     * @param \ReflectionClass $reflection
50
     * @return array
51
     */
52 4
    private function constants(\ReflectionClass $reflection): array
53
    {
54 4
        if (!$this->isFinalParentEnumInstance($reflection)) {
55 1
            throw new \LogicException('Class must be instance of Enum');
56
        }
57
58 3
        $constants = $reflection->getConstants();
59 3
        if (empty($constants)) {
60 2
            throw new \LengthException('Not exist constants in this Enum class');
61
        }
62
63 1
        return $constants;
64
    }
65
66
    /**
67
     * @param \ReflectionClass $reflection
68
     * @return bool
69
     */
70 4
    private function isFinalParentEnumInstance(\ReflectionClass $reflection): bool
71
    {
72 4
        $maxLoop = 100;
73 4
        while (($parent = $reflection->getParentClass()) && --$maxLoop > 0)
74
        {
75 3
            if ($parent->getName() === Enum::class) {
76 3
                return true;
77
            }
78
79 1
            $reflection = $parent;
80
        }
81
82 1
        return false;
83
    }
84
85
    /**
86
     * @param array $arguments
87
     * @return string
88
     */
89 5
    private function handleArguments(array $arguments): string
90
    {
91 5
        if (isset($arguments[1])) {
92 4
            return $arguments[1];
93
        }
94
95 1
        return '';
96
    }
97
98
    /**
99
     * @param string $shortClassName
100
     * @param string $nameSpace
101
     */
102 1
    private function printHeader(string $shortClassName, string $nameSpace)
103
    {
104 1
        echo "/**".PHP_EOL;
105 1
        echo " * Class $shortClassName".PHP_EOL;
106 1
        echo " * @package $nameSpace".PHP_EOL;
107 1
        $this->printBlankLine();
108 1
    }
109
110
    /**
111
     * @param array  $constants
112
     * @param string $shortClassName
113
     */
114 1
    private function printMethods(array $constants, string $shortClassName)
115
    {
116 1
        $this->staticMethodsForConstants($constants, $shortClassName);
117 1
        $this->printBlankLine();
118 1
        $this->conditionalMethodsForConstants($constants);
119 1
    }
120
121 1
    private function printBlankLine()
122
    {
123 1
        echo " * ".PHP_EOL;
124 1
    }
125
126 1
    private function printFooter()
127
    {
128 1
        echo " */".PHP_EOL;
129 1
    }
130
131
    /**
132
     * @param array  $constants
133
     * @param string $shortClassName
134
     */
135 1
    private function staticMethodsForConstants(array $constants, string $shortClassName)
136
    {
137 1
        foreach ($constants as $constantName => $constantValue) {
138 1
            $function = $this->snakeCaseToCamelCase($constantName);
139
140 1
            echo " * @method static {$shortClassName} {$function}()".PHP_EOL;
141
        }
142 1
    }
143
144
    /**
145
     * @param array $constants
146
     */
147 1
    private function conditionalMethodsForConstants(array $constants)
148
    {
149 1
        foreach ($constants as $constantName => $constantValue) {
150 1
            $function = $this->snakeCaseToCamelCase("IS_$constantName");
151
152 1
            echo " * @method bool $function()".PHP_EOL;
153
        }
154 1
    }
155
156
    /**
157
     * @param string $string
158
     * @return string
159
     */
160
    private function snakeCaseToCamelCase(string $string): string
161
    {
162 1
        return preg_replace_callback('/_(.?)/', function($matches) {
163 1
            return ucfirst($matches[1]);
164 1
        }, strtolower($string));
165
    }
166
167
    /**
168
     * @param string $errorMessage
169
     */
170 4
    private function printError(string $errorMessage)
171
    {
172 4
        echo "\e[31;1mERROR\e[0m: $errorMessage".PHP_EOL;;
173 4
    }
174
}
175