Completed
Pull Request — master (#20)
by Anatoliy
29:38 queued 19:40
created

TypeEnumBuilder::rebuild()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: danchukas
6
 * Date: 2017-07-18 12:17
7
 */
8
9
namespace DanchukAS\Mock;
10
11
12
class TypeEnumBuilder
13
{
14
    // available types
15
    protected static $boolean = "boolean";
16
    protected static $integer = "integer";
17
    protected static $double = "double";
18
    protected static $string = "string";
19
    protected static $array = "array";
20
    protected static $object = "object";
21
    protected static $resource = "resource";
22
    protected static $null = "null";
23
    protected static $unknown = "unknown";
24
25
    public static function rebuild()
26
    {
27
        $typeList = self::getTypeList();
28
29
        $property_list = [];
30
31
        foreach ($typeList as $type) {
32
            $property_name = strtoupper($type);
33
            if (5 > strlen($type)) {
34
                $no_inspect_name = "/** @noinspection PhpConstantNamingConventionInspection */\n\t\t";
35
                $property_name = $no_inspect_name . $property_name;
36
            }
37
            $property_list[] = "public const $property_name = \"$type\";";
38
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
39
        }
40
41
        $property_code = implode("\n\t", $property_list);
42
43
        $template = file_get_contents(__DIR__ . '/TypeEnum.tpl');
44
45
        $code = str_replace("{PROPERTY}", $property_code, $template);
46
47
        file_put_contents(__DIR__ . "/TypeEnum.php", $code);
48
49
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
50
51
    /**
52
     * @return array
53
     */
54
    private static function getTypeList()
55
    {
56
        $typeList = [];
57
58
        $type_dir = __DIR__ . "/Type";
59
        $directory = scandir($type_dir);
60
61
        foreach ($directory as $file) {
62
            if (is_file($type_dir . '/' . $file)) {
63
                $type = basename($file, "TypeMock.php");
64
                $typeList[] = strtolower($type);
65
            }
66
        }
67
68
        return $typeList;
69
    }
70
71
}