Passed
Pull Request — 5.1 (#192)
by
unknown
03:02
created

getSafeVariableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\TDBM\Utils;
5
6
use Doctrine\DBAL\Schema\Table;
7
use Zend\Code\Generator\DocBlock\Tag\ParamTag;
8
use Zend\Code\Generator\MethodGenerator;
9
10
/**
11
 * This class represent a property in a bean (a property has a getter, a setter, etc...).
12
 */
13
abstract class AbstractBeanPropertyDescriptor implements MethodDescriptorInterface
14
{
15
    /**
16
     * @var Table
17
     */
18
    protected $table;
19
20
    /**
21
     * Whether to use the more complex name in case of conflict.
22
     *
23
     * @var bool
24
     */
25
    protected $alternativeName = false;
26
    /**
27
     * @var NamingStrategyInterface
28
     */
29
    protected $namingStrategy;
30
31
    /**
32
     * @param Table $table
33
     * @param NamingStrategyInterface $namingStrategy
34
     */
35
    public function __construct(Table $table, NamingStrategyInterface $namingStrategy)
36
    {
37
        $this->table = $table;
38
        $this->namingStrategy = $namingStrategy;
39
    }
40
41
    /**
42
     * Use the more complex name in case of conflict.
43
     */
44
    public function useAlternativeName(): void
45
    {
46
        $this->alternativeName = true;
47
    }
48
49
    /**
50
     * Returns the name of the class linked to this property or null if this is not a foreign key.
51
     *
52
     * @return null|string
53
     */
54
    abstract public function getClassName(): ?string;
55
56
    /**
57
     * Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....)
58
     *
59
     * @return string
60
     */
61
    abstract public function getPhpType(): string;
62
63
    /**
64
     * Returns the param annotation for this property (useful for constructor).
65
     *
66
     * @return ParamTag
67
     */
68
    public function getParamAnnotation(): ParamTag
69
    {
70
        return new ParamTag($this->getVariableName(), [ $this->getPhpType() ]);
71
    }
72
73
    public function getVariableName(): string
74
    {
75
        return $this->namingStrategy->getVariableName($this);
76
    }
77
78
    public function getSafeVariableName(): string
79
    {
80
        return '$' . StringUtils::getValidVariableName(ltrim($this->getVariableName(), '$'));
81
    }
82
83
    public function getSetterName(): string
84
    {
85
        return $this->namingStrategy->getSetterName($this);
86
    }
87
88
    /**
89
     * Alias of the method getGetterName(). Used to validate MethodDescriptorInterface
90
     */
91
    public function getName(): string
92
    {
93
        return $this->getGetterName();
94
    }
95
96
    public function getGetterName(): string
97
    {
98
        return $this->namingStrategy->getGetterName($this);
99
    }
100
101
    /**
102
     * Returns the PHP code used in the ben constructor for this property.
103
     *
104
     * @return string
105
     */
106
    public function getConstructorAssignCode(): string
107
    {
108
        $str = '$this->%s(%s);';
109
110
        return sprintf($str, $this->getSetterName(), $this->getSafeVariableName());
111
    }
112
113
    /**
114
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
115
     *
116
     * @return bool
117
     */
118
    abstract public function isCompulsory(): bool;
119
120
    /**
121
     * Returns true if the property has a default value.
122
     *
123
     * @return bool
124
     */
125
    abstract public function hasDefault(): bool;
126
127
    /**
128
     * Returns the code that assigns a value to its default value.
129
     *
130
     * @return string
131
     */
132
    abstract public function assignToDefaultCode(): string;
133
134
    /**
135
     * Returns true if the property is the primary key.
136
     *
137
     * @return bool
138
     */
139
    abstract public function isPrimaryKey(): bool;
140
141
    /**
142
     * @return Table
143
     */
144
    public function getTable(): Table
145
    {
146
        return $this->table;
147
    }
148
149
    /**
150
     * Returns the PHP code for getters and setters.
151
     *
152
     * @return MethodGenerator[]
153
     */
154
    abstract public function getGetterSetterCode(): array;
155
156
    /**
157
     * Returns the part of code useful when doing json serialization.
158
     *
159
     * @return string
160
     */
161
    abstract public function getJsonSerializeCode(): string;
162
163
    /**
164
     * @return bool
165
     */
166
    public function isAlternativeName(): bool
167
    {
168
        return $this->alternativeName;
169
    }
170
171
    /**
172
     * The code to past in the __clone method.
173
     * @return null|string
174
     */
175
    abstract public function getCloneRule(): ?string;
176
177
    /**
178
     * Tells if this property is a type-hintable in PHP (resource isn't for example)
179
     *
180
     * @return bool
181
     */
182
    abstract public function isTypeHintable() : bool;
183
}
184