Completed
Pull Request — master (#4226)
by Craig
04:58
created

Validators::getReservedWords()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 69
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 71
rs 8.6763

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle\Maker;
15
16
use InvalidArgumentException;
17
18
class Validators
19
{
20
    public static function validateBundleNamespace(string $namespace, $allowSuffix = false): string
21
    {
22
        $namespace = trim(str_replace('/', '\\', $namespace));
23
        if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\?)+$/', $namespace)) {
24
            throw new InvalidArgumentException('The namespace contains invalid characters.');
25
        }
26
27
        // validate reserved keywords
28
        $reserved = self::getReservedWords();
29
        foreach (explode('\\', $namespace) as $word) {
30
            if (in_array(mb_strtolower($word), $reserved, true)) {
31
                throw new InvalidArgumentException(sprintf('The namespace cannot contain reserved words ("%s").', $word));
32
            }
33
        }
34
35
        if (!$allowSuffix) {
36
            foreach (['module', 'theme', 'bundle'] as $word) {
37
                if (false !== mb_strpos(mb_strtolower($namespace), $word)) {
38
                    throw new InvalidArgumentException(sprintf('The namespace cannot contain "%s".', $word));
39
                }
40
            }
41
        }
42
43
        // validate that the namespace is at least one level deep
44
        if (false === mb_strpos($namespace, '\\')) {
45
            throw new InvalidArgumentException(sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\%s" instead of simply "%s").', $namespace, $namespace));
46
        }
47
48
        if (mb_substr_count($namespace, '\\') > 1) {
49
            throw new InvalidArgumentException(sprintf('The namespace must contain only a vendor and BundleName (e.g. "VendorName\BundleName" instead of "%s").', $namespace));
50
        }
51
52
        return $namespace;
53
    }
54
55
    public static function getReservedWords(): array
56
    {
57
        return [
58
            'abstract',
59
            'and',
60
            'array',
61
            'as',
62
            'break',
63
            'case',
64
            'catch',
65
            'class',
66
            'clone',
67
            'const',
68
            'continue',
69
            'declare',
70
            'default',
71
            'do',
72
            'else',
73
            'elseif',
74
            'enddeclare',
75
            'endfor',
76
            'endforeach',
77
            'endif',
78
            'endswitch',
79
            'endwhile',
80
            'extends',
81
            'final',
82
            'for',
83
            'foreach',
84
            'function',
85
            'global',
86
            'goto',
87
            'if',
88
            'implements',
89
            'interface',
90
            'instanceof',
91
            'namespace',
92
            'new',
93
            'or',
94
            'private',
95
            'protected',
96
            'public',
97
            'static',
98
            'switch',
99
            'throw',
100
            'try',
101
            'use',
102
            'var',
103
            'while',
104
            'xor',
105
            '__CLASS__',
106
            '__DIR__',
107
            '__FILE__',
108
            '__LINE__',
109
            '__FUNCTION__',
110
            '__METHOD__',
111
            '__NAMESPACE__',
112
            'die',
113
            'echo',
114
            'empty',
115
            'exit',
116
            'eval',
117
            'include',
118
            'include_once',
119
            'isset',
120
            'list',
121
            'require',
122
            'require_once',
123
            'return',
124
            'print',
125
            'unset'
126
        ];
127
    }
128
}
129