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 (#77)
by joseph
15:21
created

AbstractCommand::getProjectRootNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
7
use EdmondsCommerce\DoctrineStaticMeta\Config;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
13
abstract class AbstractCommand extends Command
14
{
15
    public const COMMAND_PREFIX = 'dsm:';
16
17
    public const OPT_PROJECT_ROOT_NAMESPACE            = 'project-root-namespace';
18
    public const OPT_PROJECT_ROOT_NAMESPACE_SHORT      = 'r';
19
    public const DEFINITION_PROJECT_ROOT_NAMESPACE     = 'The root namespace for the project for which you are building'
20
                                                         . ' entities. The entities root namespace is suffixed to the '
21
                                                         . 'end of this';
22
    public const DEFAULT_PROJECT_ROOT_NAMESPACE_METHOD = 'getProjectRootNamespace';
23
24
    public const OPT_PROJECT_ROOT_PATH            = 'project-root-path';
25
    public const OPT_PROJECT_ROOT_PATH_SHORT      = 'p';
26
    public const DEFINITION_PROJECT_ROOT_PATH     = 'the filesystem path to the folder for the project. '
27
                                                    . 'This would be the folder that generally has a subfolder `src` '
28
                                                    . 'and a sub folder `tests`';
29
    public const DEFAULT_PROJECT_ROOT_PATH_METHOD = 'getProjectRootPath';
30
31
    public const OPT_SRC_SUBFOLDER        = 'src-sub-folder';
32
    public const OPT_SRC_SUBFOLDER_SHORT  = 's';
33
    public const DEFINITION_SRC_SUBFOLDER = 'The name of the subdfolder that contains sources. '
34
                                            . 'Generally this is `src` which is the default';
35
    public const DEFAULT_SRC_SUBFOLDER    = 'src';
36
37
    public const OPT_TEST_SUBFOLDER        = 'test-sub-folder';
38
    public const OPT_TEST_SUBFOLDER_SHORT  = 't';
39
    public const DEFINITION_TEST_SUBFOLDER = 'The name of the subdfolder that contains tests. '
40
                                             . 'Generally this is `tests` which is the default';
41
    public const DEFAULT_TEST_SUBFOLDER    = 'tests';
42
43
    /**
44
     * @var NamespaceHelper
45
     */
46
    protected $namespaceHelper;
47
48
    /**
49
     * AbstractCommand constructor.
50
     *
51
     * @param NamespaceHelper $namespaceHelper
52
     * @param null|string     $name
53
     *
54
     * @throws DoctrineStaticMetaException
55
     */
56 12
    public function __construct(NamespaceHelper $namespaceHelper, ?string $name = null)
57
    {
58 12
        $this->namespaceHelper = $namespaceHelper;
59
        try {
60 12
            parent::__construct($name);
61
        } catch (\Exception $e) {
62
            throw new DoctrineStaticMetaException(
63
                'Exception in ' . __METHOD__,
64
                $e->getCode(),
65
                $e
66
            );
67
        }
68 12
    }
69
70
71
    /**
72
     * @return EntityManager
73
     * @throws DoctrineStaticMetaException
74
     */
75 1
    protected function getEntityManager(): EntityManager
76
    {
77
        try {
78 1
            return $this->getHelper('em')->getEntityManager();
79
        } catch (\Exception $e) {
80
            throw new DoctrineStaticMetaException(
81
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
82
                $e->getCode(),
83
                $e
84
            );
85
        }
86
    }
87
88
    /**
89
     * @param InputInterface $input
90
     *
91
     * @throws DoctrineStaticMetaException
92
     */
93 11
    protected function checkOptions(InputInterface $input): void
94
    {
95 11
        $errors  = [];
96 11
        $options = $this->getDefinition()->getOptions();
97 11
        foreach ($options as $option) {
98 11
            $name  = $option->getName();
99 11
            $value = $input->getOption($name);
100 11
            $this->checkOptionRequired($option, $value, $name, $errors);
101 11
            if (\is_array($value)) {
102 1
                foreach ($value as $v) {
103
                    $this->checkValueForEquals($v, $name, $errors);
104
                }
105 1
                continue;
106
            }
107 11
            $this->checkValueForEquals($value, $name, $errors);
108
        }
109 11
        if (count($errors) > 0) {
110
            throw new DoctrineStaticMetaException(implode("\n\n", $errors));
111
        }
112 11
    }
113
114
    /**
115
     * @param InputOption $option
116
     * @param mixed       $value
117
     * @param string      $name
118
     * @param array       $errors
119
     */
120 11
    protected function checkOptionRequired(InputOption $option, $value, string $name, array &$errors): void
121
    {
122 11
        if ($option->isValueRequired() && (
123 11
                $value === null
124 11
                || $value === ''
125 11
                || ($option->isArray() && $value === [])
126
            )
127
        ) {
128
            $errors[] = sprintf('The required option --%s is not set or is empty', $name);
129
        }
130 11
    }
131
132
    /**
133
     * @param mixed  $value
134
     * @param string $name
135
     * @param array  $errors
136
     */
137 11
    protected function checkValueForEquals($value, string $name, array &$errors): void
138
    {
139 11
        if (\is_string($value) && '' !== $value && \ts\stringStartsWith($value, '=')) {
140
            $errors[] = 'Value for ' . $name . ' is ' . $value
141
                        . ' and starts with =, if use short options, you should not use an = sign';
142
        }
143 11
    }
144
145
    /**
146
     * Getter for self::DEFAULT_PROJECT_ROOT_PATH
147
     *
148
     * @return string
149
     * @throws DoctrineStaticMetaException
150
     * @SuppressWarnings(PHPMD.StaticAccess)
151
     */
152 11
    protected function getProjectRootPath(): string
153
    {
154
        try {
155 11
            return Config::getProjectRootDirectory();
156
        } catch (\Exception $e) {
157
            throw new DoctrineStaticMetaException(
158
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
159
                $e->getCode(),
160
                $e
161
            );
162
        }
163
    }
164
165
    /**
166
     * @param string $dirForNamespace
167
     *
168
     * @return string
169
     * @throws DoctrineStaticMetaException
170
     */
171 11
    protected function getProjectRootNamespace(string $dirForNamespace = 'src'): string
172
    {
173 11
        return (new NamespaceHelper())->getProjectRootNamespaceFromComposerJson($dirForNamespace);
174
    }
175
176
    /**
177
     * @return InputOption
178
     * @throws DoctrineStaticMetaException
179
     */
180 11
    protected function getProjectRootPathOption(): InputOption
181
    {
182
        try {
183 11
            return new InputOption(
184 11
                self::OPT_PROJECT_ROOT_PATH,
185 11
                self::OPT_PROJECT_ROOT_PATH_SHORT,
186 11
                InputOption::VALUE_OPTIONAL,
187 11
                self::DEFINITION_PROJECT_ROOT_PATH,
188 11
                $this->{self::DEFAULT_PROJECT_ROOT_PATH_METHOD}()
189
            );
190
        } catch (\Exception $e) {
191
            throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e);
192
        }
193
    }
194
195
    /**
196
     * @return InputOption
197
     * @throws DoctrineStaticMetaException
198
     */
199 11
    protected function getProjectRootNamespaceOption(): InputOption
200
    {
201
        try {
202 11
            return new InputOption(
203 11
                self::OPT_PROJECT_ROOT_NAMESPACE,
204 11
                self::OPT_PROJECT_ROOT_NAMESPACE_SHORT,
205 11
                InputOption::VALUE_REQUIRED,
206 11
                self::DEFINITION_PROJECT_ROOT_NAMESPACE,
207 11
                $this->{self::DEFAULT_PROJECT_ROOT_NAMESPACE_METHOD}()
208
            );
209
        } catch (\Exception $e) {
210
            throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e);
211
        }
212
    }
213
214
215
    /**
216
     * @return InputOption
217
     * @throws DoctrineStaticMetaException
218
     */
219 9
    protected function getSrcSubfolderOption(): InputOption
220
    {
221
        try {
222 9
            return new InputOption(
223 9
                self::OPT_SRC_SUBFOLDER,
224 9
                self::OPT_SRC_SUBFOLDER_SHORT,
225 9
                InputOption::VALUE_REQUIRED,
226 9
                self::DEFINITION_SRC_SUBFOLDER,
227 9
                self::DEFAULT_SRC_SUBFOLDER
228
            );
229
        } catch (\Exception $e) {
230
            throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e);
231
        }
232
    }
233
234
    /**
235
     * @return InputOption
236
     * @throws DoctrineStaticMetaException
237
     */
238 5
    protected function getTestSubFolderOption(): InputOption
239
    {
240
        try {
241 5
            return new InputOption(
242 5
                self::OPT_TEST_SUBFOLDER,
243 5
                self::OPT_TEST_SUBFOLDER_SHORT,
244 5
                InputOption::VALUE_REQUIRED,
245 5
                self::DEFINITION_TEST_SUBFOLDER,
246 5
                self::DEFAULT_TEST_SUBFOLDER
247
            );
248
        } catch (\Exception $e) {
249
            throw new DoctrineStaticMetaException('Exception getting option', $e->getCode(), $e);
250
        }
251
    }
252
}
253