Completed
Push — master ( 4b2870...b9812c )
by Cheren
02:31
created

CodeStyle   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 171
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 30 2
B testCyrillic() 0 26 3
A testHeadersPO() 0 17 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\TestSuite;
17
18
use JBZoo\PHPUnit\Exception;
19
use Symfony\Component\Finder\Finder;
20
use JBZoo\PHPUnit\Codestyle as JBCodeStyle;
21
22
/**
23
 * Class CodeStyle
24
 *
25
 * @package Core\TestSuite
26
 */
27
class CodeStyle extends JBCodeStyle
28
{
29
30
    /**
31
     * Package name.
32
     *
33
     * @var string
34
     */
35
    protected $_packageName = ''; // Overload me!
36
37
    /**
38
     * Package vendor.
39
     *
40
     * @var string
41
     */
42
    protected $_packageVendor = 'CakeCMS';
43
44
    /**
45
     * Package link.
46
     *
47
     * @var string
48
     */
49
    protected $_packageLink = 'https://github.com/CakeCMS/_PACKAGE_';
50
51
    /**
52
     * Package copyright.
53
     *
54
     * @var string
55
     */
56
    protected $_packageCopyright = 'MIT License http://www.opensource.org/licenses/mit-license.php';
57
58
    /**
59
     * Package description.
60
     *
61
     * @var array
62
     */
63
    protected $_packageDesc = [
64
        'This file is part of the of the simple cms based on CakePHP 3.',
65
        'For the full copyright and license information, please view the LICENSE',
66
        'file that was distributed with this source code.'
67
    ];
68
69
    /**
70
     * Ignore list for.
71
     *
72
     * @var array
73
     */
74
    protected $_excludePaths = array(
75
        '.git',
76
        '.idea',
77
        'bin',
78
        'application',
79
        'bower_components',
80
        'build',
81
        'fonts',
82
        'fixtures',
83
        'logs',
84
        'node_modules',
85
        'resources',
86
        'vendor',
87
        'temp',
88
        'tmp',
89
        'webroot/css/cache'
90
    );
91
92
    /**
93
     * Valid header for PO gettext files.
94
     *
95
     * @var array
96
     */
97
    protected $_validHeaderPO = array(
98
        '#',
99
        '# _VENDOR_ _PACKAGE_',
100
        '#',
101
        '# _DESCRIPTION_PO_',
102
        '#',
103
        '# @package   _PACKAGE_',
104
        '# @license   _LICENSE_',
105
        '# @copyright _COPYRIGHTS_',
106
        '# @link      _LINK_',
107
    );
108
109
    /**
110
     * @throws \Exception
111
     */
112
    public function setUp()
113
    {
114
        parent::setUp();
115
116
        //@codeCoverageIgnoreStart
117
        if (!$this->_packageName) {
118
            throw new Exception('$this->_packageName is undefined!');
119
        }
120
        //@codeCoverageIgnoreEnd
121
122
        $this->_replace = array(
123
            '_LINK_'                 => $this->_packageLink,
124
            '_NAMESPACE_'            => '_VENDOR_\_PACKAGE_',
125
            '_COPYRIGHTS_'           => $this->_packageCopyright,
126
            '_PACKAGE_'              => $this->_packageName,
127
            '_LICENSE_'              => $this->_packageLicense,
128
            '_AUTHOR_'               => $this->_packageAuthor,
129
            '_VENDOR_'               => $this->_packageVendor,
130
            '_DESCRIPTION_PHP_'      => implode($this->_le . ' * ', $this->_packageDesc),
131
            '_DESCRIPTION_JS_'       => implode($this->_le . ' * ', $this->_packageDesc),
132
            '_DESCRIPTION_CSS_'      => implode($this->_le . ' * ', $this->_packageDesc),
133
            '_DESCRIPTION_LESS_'     => implode($this->_le . '// ', $this->_packageDesc),
134
            '_DESCRIPTION_XML_'      => implode($this->_le . '    ', $this->_packageDesc),
135
            '_DESCRIPTION_INI_'      => implode($this->_le . '; ', $this->_packageDesc),
136
            '_DESCRIPTION_SH_'       => implode($this->_le . '# ', $this->_packageDesc),
137
            '_DESCRIPTION_PO_'       => implode($this->_le . '# ', $this->_packageDesc),
138
            '_DESCRIPTION_SQL_'      => implode($this->_le . '-- ', $this->_packageDesc),
139
            '_DESCRIPTION_HTACCESS_' => implode($this->_le . '# ', $this->_packageDesc),
140
        );
141
    }
142
143
    /**
144
     * Try to find cyrilic symbols in the code.
145
     *
146
     * @return void
147
     */
148
    public function testCyrillic()
149
    {
150
        $finder = new Finder();
151
        $finder
152
            ->files()
153
            ->in(PROJECT_ROOT)
154
            ->exclude($this->_excludePaths)
155
            ->exclude('tests')
156
            ->notPath(basename(__FILE__))
157
            ->notName('/\.md$/')
158
            ->notName('/\.po$/')
159
            ->notName('/empty/')
160
            ->notName('/\.min\.(js|css)$/')
161
            ->notName('/\.min\.(js|css)\.map$/');
162
163
        /** @var \SplFileInfo $file */
164
        foreach ($finder as $file) {
165
            $content = \JBZoo\PHPUnit\openFile($file->getPathname());
166
167
            if (preg_match('#[А-Яа-яЁё]#ius', $content)) {
168
                \JBZoo\PHPUnit\fail('File contains cyrilic symbols: ' . $file); // Short message in terminal
169
            } else {
170
                \JBZoo\PHPUnit\success();
171
            }
172
        }
173
    }
174
175
    /**
176
     * Test copyright headers of PO files.
177
     *
178
     * @return void
179
     */
180
    public function testHeadersPO()
181
    {
182
        $valid = $this->_prepareTemplate(implode($this->_validHeaderPO, $this->_le));
183
184
        $finder = new Finder();
185
        $finder
186
            ->files()
187
            ->in(PROJECT_ROOT)
188
            ->exclude($this->_excludePaths)
189
            ->name('*.po');
190
191
        /** @var \SplFileInfo $file */
192
        foreach ($finder as $file) {
193
            $content = \JBZoo\PHPUnit\openFile($file->getPathname());
194
            \JBZoo\PHPUnit\isContain($valid, $content, false, 'File has no valid header: ' . $file);
195
        }
196
    }
197
}
198