PhpClass   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 180
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 29

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAbstract() 0 3 1
A setAbstract() 0 5 1
A getPhpInterfaces() 0 8 3
A getPhpAbstract() 0 3 2
A interfaceIsValid() 0 3 2
A getPhpDeclaration() 0 3 1
A setInterfaces() 0 8 2
A getPhpInterface() 0 3 2
A getInterfaces() 0 3 1
A interfacesAreValid() 0 8 2
A getPhpExtends() 0 5 3
A setExtends() 0 8 2
A getChildrenTypes() 0 8 1
A useBracketsForChildren() 0 3 1
A extendsIsValid() 0 3 3
A getExtends() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PhpGenerator\Element;
6
7
use InvalidArgumentException;
8
9
class PhpClass extends AbstractElement
10
{
11
    public const PHP_DECLARATION = 'class';
12
13
    public const PHP_ABSTRACT_KEYWORD = 'abstract';
14
15
    public const PHP_IMPLEMENTS_KEYWORD = 'implements';
16
17
    public const PHP_EXTENDS_KEYWORD = 'extends';
18
19
    protected bool $abstract;
20
21
    /**
22
     * @var PhpClass|string
23
     */
24
    protected $extends;
25
26
    /**
27
     * @var PhpClass[]|string[]
28
     */
29
    protected array $interfaces;
30
31 120
    public function __construct(string $name, bool $abstract = false, $extends = null, array $interfaces = [])
32
    {
33 120
        parent::__construct($name);
34
        $this
35 120
            ->setAbstract($abstract)
36 120
            ->setExtends($extends)
37 120
            ->setInterfaces($interfaces)
38
        ;
39 120
    }
40
41 120
    public function setAbstract(bool $abstract): self
42
    {
43 120
        $this->abstract = $abstract;
44
45 120
        return $this;
46
    }
47
48 50
    public function getAbstract(): bool
49
    {
50 50
        return $this->abstract;
51
    }
52
53
    /**
54
     * @param null|PhpClass|string $extends
55
     *
56
     * @throws InvalidArgumentException
57
     */
58 120
    public function setExtends($extends): self
59
    {
60 120
        if (!static::extendsIsValid($extends)) {
61 2
            throw new InvalidArgumentException('Extends must be a string or a PhpClass instance');
62
        }
63 120
        $this->extends = $extends;
64
65 120
        return $this;
66
    }
67
68
    /**
69
     * @param null|PhpClass|string $extends
70
     */
71 120
    public static function extendsIsValid($extends): bool
72
    {
73 120
        return is_null($extends) || static::stringIsValid($extends, true, true) || $extends instanceof PhpClass;
74
    }
75
76
    /**
77
     * @return null|PhpClass|string
78
     */
79 52
    public function getExtends()
80
    {
81 52
        return $this->extends;
82
    }
83
84
    /**
85
     * @param PhpClass[]|string[] $interfaces
86
     *
87
     * @throws InvalidArgumentException
88
     */
89 120
    public function setInterfaces(array $interfaces = []): self
90
    {
91 120
        if (!static::interfacesAreValid($interfaces)) {
92 2
            throw new InvalidArgumentException('Interfaces are not valid');
93
        }
94 120
        $this->interfaces = $interfaces;
95
96 120
        return $this;
97
    }
98
99
    /**
100
     * @param PhpClass[]|string[] $interfaces
101
     */
102 120
    public static function interfacesAreValid(array $interfaces = []): bool
103
    {
104 120
        $valid = true;
105 120
        foreach ($interfaces as $interface) {
106 28
            $valid &= static::interfaceIsValid($interface);
107
        }
108
109 120
        return (bool) $valid;
110
    }
111
112
    /**
113
     * @param PhpClass|string $interface
114
     */
115 28
    public static function interfaceIsValid($interface): bool
116
    {
117 28
        return static::stringIsValid($interface) || $interface instanceof PhpClass;
118
    }
119
120
    /**
121
     * @return PhpClass[]|string[]
122
     */
123 68
    public function getInterfaces(): array
124
    {
125 68
        return $this->interfaces;
126
    }
127
128 66
    public function getPhpDeclaration(): string
129
    {
130 66
        return trim(sprintf('%s%s %s%s%s', $this->getPhpAbstract(), static::PHP_DECLARATION, $this->getPhpName(), $this->getPhpExtends(), $this->getPhpInterfaces()));
131
    }
132
133
    /**
134
     * defines authorized children element types.
135
     *
136
     * @return string[]
137
     */
138 32
    public function getChildrenTypes(): array
139
    {
140
        return [
141 32
            'string',
142
            PhpAnnotationBlock::class,
143
            PhpMethod::class,
144
            PhpConstant::class,
145
            PhpProperty::class,
146
        ];
147
    }
148
149
    /**
150
     * Allows to indicate that children are contained by brackets,
151
     * in the case the method returns true, getBracketBeforeChildren
152
     * is called instead of getLineBeforeChildren and getBracketAfterChildren
153
     * is called instead of getLineAfterChildren, but be aware that these methods
154
     * call the two others.
155
     */
156 34
    public function useBracketsForChildren(): bool
157
    {
158 34
        return true;
159
    }
160
161 66
    protected function getPhpAbstract(): string
162
    {
163 66
        return !$this->getAbstract() ? '' : static::PHP_ABSTRACT_KEYWORD.' ';
164
    }
165
166 66
    protected function getPhpExtends(): string
167
    {
168 66
        $extends = $this->getExtends();
169
170 66
        return empty($extends) ? '' : sprintf(' %s %s', static::PHP_EXTENDS_KEYWORD, ($extends instanceof PhpClass ? $extends->getName() : $extends));
171
    }
172
173 66
    protected function getPhpInterfaces(): string
174
    {
175 66
        $interfaces = [];
176 66
        foreach ($this->getInterfaces() as $interface) {
177 24
            $interfaces[] = $this->getPhpInterface($interface);
178
        }
179
180 66
        return empty($interfaces) ? '' : sprintf(' %s%s', static::PHP_IMPLEMENTS_KEYWORD, implode(',', $interfaces));
181
    }
182
183
    /**
184
     * @param PhpClass|string $interface
185
     */
186 24
    protected function getPhpInterface($interface): string
187
    {
188 24
        return sprintf(' %s', is_string($interface) ? $interface : $interface->getName());
189
    }
190
}
191