Passed
Pull Request — master (#809)
by Stanislav
06:02
created

GlueControllerFinder::toSingular()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Zed\DocumentationGeneratorRestApi\Business\Finder;
11
12
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRoutePluginInterface;
13
use Spryker\Zed\DocumentationGeneratorRestApi\Business\Finder\GlueControllerFinder as SprykerGlueControllerFinder;
14
15
class GlueControllerFinder extends SprykerGlueControllerFinder
16
{
17
    /**
18
     * @param \Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\ResourceRoutePluginInterface $plugin
19
     *
20
     * @return array<\SplFileInfo>
21
     */
22
    public function getGlueControllerFilesFromPlugin(ResourceRoutePluginInterface $plugin): array
23
    {
24
        $controllerNamespace = $this->getPluginControllerClass($plugin);
25
        $controllerNamespaceExploded = explode('\\', $controllerNamespace);
26
27
        $controllerIndex = array_search('Controller', $controllerNamespaceExploded, true);
28
        $moduleName = ($controllerIndex !== false && $controllerIndex > 0) ? $controllerNamespaceExploded[$controllerIndex - 1] : array_slice($controllerNamespaceExploded, -3)[0];
29
30
        $existingDirectories = $this->getMonoRepositoryControllerSourceDirectories($moduleName, $moduleName);
31
        if (!$existingDirectories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $existingDirectories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
            $existingDirectories = $this->getMonoRepositoryControllerSourceDirectories(
33
                $this->toSingular($this->removeRestApiSuffix($moduleName)),
34
                $moduleName,
35
            );
36
        }
37
38
        if (!$existingDirectories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $existingDirectories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
39
            return [];
40
        }
41
42
        $finder = clone $this->finder;
43
        $finder->in($existingDirectories)->name(sprintf(static::PATTERN_CONTROLLER_FILENAME, end($controllerNamespaceExploded)));
44
45
        return iterator_to_array($finder);
46
    }
47
48
    /**
49
     * @param string $moduleDirectory
50
     * @param string $moduleName
51
     *
52
     * @return array<string>
53
     */
54
    protected function getMonoRepositoryControllerSourceDirectories(string $moduleDirectory, string $moduleName): array
55
    {
56
        $directories = array_map(function ($directory) use ($moduleDirectory, $moduleName) {
57
            return sprintf($directory, $moduleDirectory, $moduleName);
58
        }, $this->sourceDirectories);
59
60
        return $this->getExistingSourceDirectories($directories);
61
    }
62
63
    protected function removeRestApiSuffix(string $moduleName): string
64
    {
65
        return str_replace('RestApi', '', $moduleName);
66
    }
67
68
    protected function toSingular(string $moduleName): string
69
    {
70
        $rules = [
71
            '/(s|x|z|ch|sh)es$/i' => '$1',
72
            '/ies$/i' => 'y',
73
            '/ves$/i' => 'f',
74
            '/s$/i' => '',
75
        ];
76
77
        foreach ($rules as $pattern => $replacement) {
78
            if (preg_match($pattern, $moduleName)) {
79
                return preg_replace($pattern, $replacement, $moduleName);
80
            }
81
        }
82
83
        return $moduleName;
84
    }
85
}
86