GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#51)
by joseph
06:58
created

FindAndReplaceHelper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 290
Duplicated Lines 0 %

Test Coverage

Coverage 85.84%

Importance

Changes 0
Metric Value
wmc 18
dl 0
loc 290
ccs 97
cts 113
cp 0.8584
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceEntityRepositoriesNamespace() 0 9 1
A findReplace() 0 10 1
A replaceProjectNamespace() 0 9 1
A replaceFieldTraitNamespace() 0 15 2
A replaceFieldInterfaceNamespace() 0 14 2
A replaceName() 0 15 1
A __construct() 0 3 1
A findReplaceRegex() 0 10 1
B setNamespaceFromPath() 0 36 2
A replacePluralName() 0 16 1
A replaceEntityNamespace() 0 12 2
A replaceEntityInterfaceNamespace() 0 9 1
A replaceEntitiesNamespace() 0 12 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
7
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
8
9
/**
10
 * Provides functionality to find and replace text in generated code
11
 *
12
 * Class FindAndReplaceHelper
13
 *
14
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator
15
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
16
 */
17
class FindAndReplaceHelper
18
{
19
20
    /**
21
     * @var NamespaceHelper
22
     */
23
    protected $namespaceHelper;
24
25 60
    public function __construct(NamespaceHelper $namespaceHelper)
26
    {
27 60
        $this->namespaceHelper = $namespaceHelper;
28 60
    }
29
30
    /**
31
     * @param string $find
32
     * @param string $replace
33
     * @param string $filePath
34
     *
35
     * @return self
36
     */
37 59
    public function findReplace(
38
        string $find,
39
        string $replace,
40
        string $filePath
41
    ): self {
42 59
        $contents = file_get_contents($filePath);
43 59
        $contents = str_replace($find, $replace, $contents);
44 59
        file_put_contents($filePath, $contents);
45
46 59
        return $this;
47
    }
48
49
    /**
50
     * @param string $find
51
     * @param string $replace
52
     * @param string $filePath
53
     *
54
     * @return self
55
     */
56 24
    public function findReplaceRegex(
57
        string $find,
58
        string $replace,
59
        string $filePath
60
    ): self {
61 24
        $contents = file_get_contents($filePath);
62 24
        $contents = preg_replace($find, $replace, $contents);
63 24
        file_put_contents($filePath, $contents);
64
65 24
        return $this;
66
    }
67
68
    /**
69
     * @param string $replacement
70
     * @param string $filePath
71
     * @param string $findName
72
     *
73
     * @return self
74
     * @SuppressWarnings(PHPMD.StaticAccess)
75
     */
76 59
    public function replaceName(
77
        string $replacement,
78
        string $filePath,
79
        $findName = AbstractGenerator::FIND_ENTITY_NAME
80
    ): self {
81 59
        $this->findReplace($findName, $replacement, $filePath);
82 59
        $this->findReplace(\lcfirst($findName), \lcfirst($replacement), $filePath);
83 59
        $this->findReplace(\strtoupper($findName), \strtoupper($replacement), $filePath);
84 59
        $this->findReplace(
85 59
            \strtoupper(Inflector::tableize($findName)),
86 59
            \strtoupper(Inflector::tableize($replacement)),
87 59
            $filePath
88
        );
89
90 59
        return $this;
91
    }
92
93
    /**
94
     * @param string $replacement
95
     * @param string $filePath
96
     *
97
     * @return self
98
     * @SuppressWarnings(PHPMD.StaticAccess)
99
     */
100 24
    public function replacePluralName(string $replacement, string $filePath): self
101
    {
102 24
        $this->findReplace(AbstractGenerator::FIND_ENTITY_NAME_PLURAL, $replacement, $filePath);
103 24
        $this->findReplace(\lcfirst(AbstractGenerator::FIND_ENTITY_NAME_PLURAL), \lcfirst($replacement), $filePath);
104 24
        $this->findReplace(
105 24
            \strtoupper(AbstractGenerator::FIND_ENTITY_NAME_PLURAL),
106 24
            \strtoupper($replacement),
107 24
            $filePath
108
        );
109 24
        $this->findReplace(
110 24
            \strtoupper(Inflector::tableize(AbstractGenerator::FIND_ENTITY_NAME_PLURAL)),
111 24
            \strtoupper(Inflector::tableize($replacement)),
112 24
            $filePath
113
        );
114
115 24
        return $this;
116
    }
117
118
    /**
119
     * @param string $replacement
120
     * @param string $filePath
121
     *
122
     * @return self
123
     * @throws \RuntimeException
124
     */
125 58
    public function replaceEntitiesNamespace(string $replacement, string $filePath): self
126
    {
127 58
        if (false === strpos($replacement, '\\Entities')) {
128
            throw new \RuntimeException('$replacement '.$replacement.' does not contain \\Entities\\');
129
        }
130 58
        $this->findReplace(
131 58
            AbstractGenerator::FIND_ENTITIES_NAMESPACE,
132 58
            $this->namespaceHelper->tidy($replacement),
133 58
            $filePath
134
        );
135
136 58
        return $this;
137
    }
138
139
    /**
140
     * @param string $replacement
141
     * @param string $filePath
142
     *
143
     * @return self
144
     * @throws \RuntimeException
145
     */
146
    public function replaceEntityNamespace(string $replacement, string $filePath): self
147
    {
148
        if (false === strpos($replacement, '\\Entity')) {
149
            throw new \RuntimeException('$replacement '.$replacement.' does not contain \\Entity\\');
150
        }
151
        $this->findReplace(
152
            AbstractGenerator::FIND_ENTITY_NAMESPACE,
153
            $this->namespaceHelper->tidy($replacement),
154
            $filePath
155
        );
156
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $replacement
162
     * @param string $filePath
163
     *
164
     * @return self
165
     * @throws \RuntimeException
166
     */
167 7
    public function replaceFieldTraitNamespace(string $replacement, string $filePath): self
168
    {
169 7
        if (false === strpos($replacement, AbstractGenerator::ENTITY_FIELD_TRAIT_NAMESPACE)) {
170
            throw new \RuntimeException(
171
                '$replacement '.$replacement.' does not contain '
172
                .AbstractGenerator::ENTITY_FIELD_TRAIT_NAMESPACE
173
            );
174
        }
175 7
        $this->findReplace(
176 7
            AbstractGenerator::FIND_FIELD_TRAIT_NAMESPACE,
177 7
            $this->namespaceHelper->tidy($replacement),
178 7
            $filePath
179
        );
180
181 7
        return $this;
182
    }
183
184
    /**
185
     * @param string $replacement
186
     * @param string $filePath
187
     *
188
     * @return self
189
     * @throws \RuntimeException
190
     */
191 7
    public function replaceFieldInterfaceNamespace(string $replacement, string $filePath): self
192
    {
193 7
        if (false === strpos($replacement, AbstractGenerator::ENTITY_FIELD_INTERFACE_NAMESPACE)) {
194
            throw new \RuntimeException(
195
                '$replacement '.$replacement.' does not contain '.AbstractGenerator::ENTITY_FIELD_INTERFACE_NAMESPACE
196
            );
197
        }
198 7
        $this->findReplace(
199 7
            AbstractGenerator::FIND_FIELD_INTERFACE_NAMESPACE,
200 7
            $this->namespaceHelper->tidy($replacement),
201 7
            $filePath
202
        );
203
204 7
        return $this;
205
    }
206
207
    /**
208
     * @param string $replacement
209
     * @param string $filePath
210
     *
211
     * @return self
212
     */
213 58
    public function replaceProjectNamespace(string $replacement, string $filePath): self
214
    {
215 58
        $this->findReplace(
216 58
            AbstractGenerator::FIND_PROJECT_NAMESPACE,
217 58
            $this->namespaceHelper->tidy($replacement),
218 58
            $filePath
219
        );
220
221 58
        return $this;
222
    }
223
224
    /**
225
     * @param string $replacement
226
     * @param string $filePath
227
     *
228
     * @return self
229
     */
230 58
    public function replaceEntityRepositoriesNamespace(string $replacement, string $filePath): self
231
    {
232 58
        $this->findReplace(
233 58
            AbstractGenerator::FIND_ENTITY_REPOSITORIES_NAMESPACE,
234 58
            $this->namespaceHelper->tidy($replacement),
235 58
            $filePath
236
        );
237
238 58
        return $this;
239
    }
240
241
    /**
242
     * @param string $replacement
243
     * @param string $filePath
244
     *
245
     * @return self
246
     */
247 58
    public function replaceEntityInterfaceNamespace(string $replacement, string $filePath): self
248
    {
249 58
        $this->findReplace(
250 58
            AbstractGenerator::FIND_ENTITY_INTERFACE_NAMESPACE,
251 58
            $this->namespaceHelper->tidy($replacement),
252 58
            $filePath
253
        );
254
255 58
        return $this;
256
    }
257
258
    /**
259
     * Totally replace the defined namespace in a class/trait
260
     * with a namespace calculated from the path of the file
261
     *
262
     * @param string $filePath
263
     *
264
     * @param string $srcSubFolderName
265
     *
266
     * @param string $projectRootNamespace
267
     *
268
     * @return self
269
     * @throws DoctrineStaticMetaException
270
     */
271 24
    public function setNamespaceFromPath(string $filePath, string $srcSubFolderName, string $projectRootNamespace): self
272
    {
273 24
        $pathForNamespace = substr(
274 24
            $filePath,
275 24
            strpos(
276 24
                $filePath,
277 24
                $srcSubFolderName
278
            )
279 24
            + \strlen($srcSubFolderName)
280 24
            + 1
281
        );
282 24
        $pathForNamespace = substr($pathForNamespace, 0, strrpos($pathForNamespace, '/'));
283
        $namespaceToSet   = $projectRootNamespace
284 24
                            .'\\'.implode(
285 24
                                '\\',
286 24
                                explode(
287 24
                                    '/',
288 24
                                    $pathForNamespace
289
                                )
290
                            );
291 24
        $contents         = file_get_contents($filePath);
292 24
        $contents         = preg_replace(
293 24
            '%namespace[^:]+?;%',
294 24
            "namespace $namespaceToSet;",
295 24
            $contents,
296 24
            1,
297 24
            $count
298
        );
299 24
        if ($count !== 1) {
300
            throw new DoctrineStaticMetaException(
301
                'Namespace replace count is '.$count.', should be 1 when updating file: '.$filePath
302
            );
303
        }
304 24
        file_put_contents($filePath, $contents);
305
306 24
        return $this;
307
    }
308
}
309