Completed
Push — master ( 3c0a55...3afa22 )
by Maxime
02:42 queued 01:12
created

TypesDumper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpToFile() 0 4 1
A dump() 0 9 2
A getTypeCode() 0 30 2
A getTypeClassname() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Doctrine\DBAL\Types;
12
13
/**
14
 * @internal
15
 */
16
class TypesDumper
17
{
18
    const MARKER = 'ELAO_ENUM_DT';
19
20
    public function dumpToFile(string $file, array $types)
21
    {
22
        file_put_contents($file, $this->dump($types));
23
    }
24
25
    private function dump(array $types): string
26
    {
27
        $code = "<?php\n";
28
        foreach ($types as [$class, $type, $name]) {
29
            $code .= $this->getTypeCode($class, $type, $name);
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
        }
31
32
        return $code;
33
    }
34
35
    private function getTypeCode(string $class, string $type, string $name): string
36
    {
37
        $baseClass = $type === 'int' ? AbstractIntegerEnumType::class : AbstractEnumType::class;
38
        $fqcn = self::getTypeClassname($class);
39
        $classname = basename(str_replace('\\', '/', $fqcn));
40
        $ns = substr($fqcn, 0, -\strlen($classname) - 1);
41
42
        return <<<PHP
43
44
namespace $ns;
45
46
if (!\class_exists('\\$fqcn')) {
47
    class $classname extends \\{$baseClass}
48
    {
49
        const NAME = '$name';
50
51
        protected function getEnumClass(): string
52
        {
53
            return \\{$class}::class;
54
        }
55
56
        public function getName(): string
57
        {
58
            return static::NAME;
59
        }
60
    }
61
}
62
63
PHP;
64
    }
65
66
    public static function getTypeClassname(string $class): string
67
    {
68
        return self::MARKER . "\\{$class}Type";
69
    }
70
}
71