• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JBZoo / Codestyle / 8445177639

26 Mar 2024 08:28PM UTC coverage: 87.66% (-0.3%) from 87.967%
8445177639

push

github

web-flow
Update getFixerConfig method in PhpCsFixerCodingStandard (#50)

11 of 14 new or added lines in 1 file covered. (78.57%)

51 existing lines in 5 files now uncovered.

547 of 624 relevant lines covered (87.66%)

10.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.4
/src/PHPUnit/TraitCopyright.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Codestyle.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Codestyle
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Codestyle\PHPUnit;
18

19
use Symfony\Component\Finder\Finder;
20

21
use function JBZoo\PHPUnit\fail;
22
use function JBZoo\PHPUnit\success;
23

24
/**
25
 * @phan-file-suppress PhanUndeclaredProperty
26
 */
27
trait TraitCopyright
28
{
29
    /** @var string[] */
30
    protected array $excludedPathsForCopyrights = [
31
        '.git',
32
        '.idea',
33
        'bower_components',
34
        'build',
35
        'fonts',
36
        'fixtures',
37
        'logs',
38
        'node_modules',
39
        'resources',
40
        'vendor',
41
        'temp',
42
        'tmp',
43
    ];
44

45
    /** @var string[] */
46
    protected array $packageDesc = [
47
        'This file is part of the _VENDOR_ project.',
48
        'For the full copyright and license information, please view the LICENSE',
49
        'file that was distributed with this source code.',
50
    ];
51

52
    // ### Patterns of copyrights. #####################################################################################
53

54
    /** @var string[] */
55
    protected array $validHeaderPHP = [
56
        '/**',
57
        ' * _VENDOR_ - _PACKAGE_.',
58
        ' *',
59
        ' * _DESCRIPTION_PHP_',
60
        ' *',
61
        ' * @license    _LICENSE_',
62
        ' * @copyright  _COPYRIGHTS_',
63
        ' * @see        _LINK_',
64
    ];
65

66
    /** @var string[] */
67
    protected array $validHeaderJS = [
68
        '/**',
69
        ' * _VENDOR_ - _PACKAGE_.',
70
        ' *',
71
        ' * _DESCRIPTION_JS_',
72
        ' *',
73
        ' * @license    _LICENSE_',
74
        ' * @copyright  _COPYRIGHTS_',
75
        ' * @see        _LINK_',
76
    ];
77

78
    /** @var string[] */
79
    protected array $validHeaderCSS = [
80
        '/**',
81
        ' * _VENDOR_ - _PACKAGE_.',
82
        ' *',
83
        ' * _DESCRIPTION_CSS_',
84
        ' *',
85
        ' * @license    _LICENSE_',
86
        ' * @copyright  _COPYRIGHTS_',
87
        ' * @see        _LINK_',
88
        ' */',
89
        '',
90
    ];
91

92
    /** @var string[] */
93
    protected array $validHeaderLESS = [
94
        '//',
95
        '// _VENDOR_ - _PACKAGE_.',
96
        '//',
97
        '// _DESCRIPTION_LESS_',
98
        '//',
99
        '// @license    _LICENSE_',
100
        '// @copyright  _COPYRIGHTS_',
101
        '// @see        _LINK_',
102
        '//',
103
    ];
104

105
    /** @var string[] */
106
    protected array $validHeaderXML = [
107
        '<?xml version="1.0" encoding="UTF-8" ?>',
108
        '<!--',
109
        '    _VENDOR_ - _PACKAGE_.',
110
        '',
111
        '    _DESCRIPTION_XML_',
112
        '',
113
        '    @license    _LICENSE_',
114
        '    @copyright  _COPYRIGHTS_',
115
        '    @see        _LINK_',
116
        '-->',
117
    ];
118

119
    /** @var string[] */
120
    protected array $validHeaderINI = [
121
        ';',
122
        '; _VENDOR_ - _PACKAGE_.',
123
        ';',
124
        '; _DESCRIPTION_INI_',
125
        ';',
126
        '; @license    _LICENSE_',
127
        '; @copyright  _COPYRIGHTS_',
128
        '; @see        _LINK_',
129
        ';',
130
        '; Note: All ini files need to be saved as UTF-8 (no BOM)',
131
        ';',
132
    ];
133

134
    /** @var string[] */
135
    protected array $validHeaderSH = [
136
        '#!/usr/bin/env sh',
137
        '',
138
        '#',
139
        '# _VENDOR_ - _PACKAGE_.',
140
        '#',
141
        '# _DESCRIPTION_SH_',
142
        '#',
143
        '# @license    _LICENSE_',
144
        '# @copyright  _COPYRIGHTS_',
145
        '# @see        _LINK_',
146
        '#',
147
        '',
148
    ];
149

150
    /** @var string[] */
151
    protected array $validHeaderSQL = [
152
        '--',
153
        '-- _VENDOR_ - _PACKAGE_.',
154
        '--',
155
        '-- _DESCRIPTION_SQL_',
156
        '--',
157
        '-- @license    _LICENSE_',
158
        '-- @copyright  _COPYRIGHTS_',
159
        '-- @see        _LINK_',
160
        '--',
161
        '',
162
    ];
163

164
    /** @var string[] */
165
    protected array $validHeaderHash = [
166
        '#',
167
        '# _VENDOR_ - _PACKAGE_.',
168
        '#',
169
        '# _DESCRIPTION_HASH_',
170
        '#',
171
        '# @license    _LICENSE_',
172
        '# @copyright  _COPYRIGHTS_',
173
        '# @see        _LINK_',
174
        '#',
175
        '',
176
    ];
177

178
    public function testHeadersPhp(): void
179
    {
180
        $phpTemplate = $this->prepareTemplate($this->validHeaderPHP);
6✔
181

182
        $phpTemplate = "<?php\n\n{$phpTemplate}" . \implode("\n", [
6✔
183
            '',
3✔
184
            ' */',
3✔
185
            '',
3✔
186
            'declare(strict_types=1);',
3✔
187
            '',
3✔
188
        ]);
3✔
189

190
        $finder = $this->createFinder(['.php', '.phtml']);
6✔
191
        static::checkHeaderInFiles($finder, $phpTemplate);
6✔
192
    }
193

194
    public function testHeadersJs(): void
195
    {
196
        $finder = $this->createFinder(['.js', '.jsx'], ['*.min.js', '*.min.jsx']);
6✔
197
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderJS));
6✔
198
    }
199

200
    public function testHeadersCss(): void
201
    {
202
        $finder = $this->createFinder(['.css'], ['*.min.css']);
6✔
203
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderCSS));
6✔
204
    }
205

206
    public function testHeadersLess(): void
207
    {
208
        $finder = $this->createFinder(['.less']);
6✔
209
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderLESS));
6✔
210
    }
211

212
    public function testHeadersXml(): void
213
    {
214
        $finder = $this->createFinder(['.xml']);
6✔
215
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderXML));
6✔
216
    }
217

218
    public function testHeadersIni(): void
219
    {
220
        $finder = $this->createFinder(['.ini']);
6✔
221
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderINI));
6✔
222
    }
223

224
    public function testHeadersSh(): void
225
    {
226
        $finder = $this->createFinder(['.sh', '.bash', '.fish']);
6✔
227
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderSH));
6✔
228
    }
229

230
    public function testHeadersSql(): void
231
    {
232
        $finder = $this->createFinder(['.sql']);
6✔
233
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderSQL));
6✔
234
    }
235

236
    public function testHeadersOtherConfigs(): void
237
    {
238
        $finder = $this->createFinder([
6✔
239
            'Dockerfile',
3✔
240
            'Makefile',
3✔
241
            '.Makefile',
3✔
242
            '.yml',
3✔
243
            '.yaml',
3✔
244
            '.neon',
3✔
245
            '.htaccess',
3✔
246
            '.editorconfig',
3✔
247
            '.gitattributes',
3✔
248
            '.gitignore',
3✔
249
        ]);
3✔
250
        static::checkHeaderInFiles($finder, $this->prepareTemplate($this->validHeaderHash));
6✔
251
    }
252

253
    // ### Internal tools for test case ################################################################################
254

255
    /**
256
     * @param array<string> $inclusions
257
     * @param array<string> $exclusions
258
     */
259
    protected function createFinder(array $inclusions = [], array $exclusions = []): Finder
260
    {
261
        $finder = (new Finder())
54✔
262
            ->files()
54✔
263
            ->in(PROJECT_ROOT)
54✔
264
            ->exclude($this->excludedPathsForCopyrights)
54✔
265
            ->ignoreDotFiles(false)
54✔
266
            ->ignoreVCS(true)
54✔
267
            ->followLinks();
54✔
268

269
        foreach ($inclusions as $inclusion) {
54✔
270
            $finder->name($inclusion);
54✔
271
            if (\str_contains($inclusion, '.')) {
54✔
272
                $finder
27✔
273
                    ->name('/\\' . $inclusion . '$/')
54✔
274
                    ->name('/dist\\' . $inclusion . '$/')
54✔
275
                    ->name('/\\' . $inclusion . '.dist$/');
54✔
276
            }
277
        }
278

279
        foreach ($exclusions as $exclusion) {
54✔
280
            $finder->notName($exclusion);
12✔
281
        }
282

283
        return $finder;
54✔
284
    }
285

286
    /**
287
     * Render copyrights.
288
     */
289
    protected function prepareTemplate(array $templateRows): string
290
    {
291
        $template = \implode("\n", $templateRows);
54✔
292

293
        // Important! Order of replacements is important!
294
        $replace = [
54✔
295
            '_DESCRIPTION_PHP_'  => \implode("\n * ", $this->packageDesc),
54✔
296
            '_DESCRIPTION_JS_'   => \implode("\n * ", $this->packageDesc),
54✔
297
            '_DESCRIPTION_CSS_'  => \implode("\n * ", $this->packageDesc),
54✔
298
            '_DESCRIPTION_LESS_' => \implode("\n// ", $this->packageDesc),
54✔
299
            '_DESCRIPTION_XML_'  => \implode("\n    ", $this->packageDesc),
54✔
300
            '_DESCRIPTION_INI_'  => \implode("\n; ", $this->packageDesc),
54✔
301
            '_DESCRIPTION_SH_'   => \implode("\n# ", $this->packageDesc),
54✔
302
            '_DESCRIPTION_SQL_'  => \implode("\n-- ", $this->packageDesc),
54✔
303
            '_DESCRIPTION_HASH_' => \implode("\n# ", $this->packageDesc),
54✔
304
            '_LINK_'             => $this->copyrightSee,
54✔
305
            '_NAMESPACE_'        => '_VENDOR_\_PACKAGE_',
27✔
306
            '_COPYRIGHTS_'       => $this->copyrightRights,
54✔
307
            '_PACKAGE_'          => $this->packageName,
54✔
308
            '_LICENSE_'          => $this->copyrightLicense,
54✔
309
            '_VENDOR_NS_'        => $this->vendorName,
54✔
310
            '_VENDOR_'           => $this->copyrightVendorName,
54✔
311
        ];
27✔
312

313
        foreach ($replace as $const => $value) {
54✔
314
            $template = \str_replace($const, $value, $template);
54✔
315
        }
316

317
        return $template;
54✔
318
    }
319

320
    /**
321
     * Checks for the presence of a valid header in files found by a Finder object.
322
     * If any invalid files are found, it generates an error indicating the incorrect files.
323
     * On successful validation of all files, it outputs a success message.
324
     * If no files are found, it outputs a skipped check message.
325
     *
326
     * @param Finder $finder      finder object used to search for files
327
     * @param string $validHeader string containing the correct header for validation
328
     */
329
    protected static function checkHeaderInFiles(Finder $finder, string $validHeader): void
330
    {
331
        $invalidFiles = [];
54✔
332

333
        $testFunction = static function (string $content, string $pathname) use ($validHeader, &$invalidFiles): void {
54✔
334
            if (!\str_starts_with($content, $validHeader)) {
54✔
335
                $invalidFiles[] = $pathname;
×
336
            }
337
        };
27✔
338

339
        $testName   = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
54✔
340
        $filesCount = AbstractPackageTest::checkFiles($testName, $finder, $testFunction);
54✔
341

342
        if (\count($invalidFiles) > 0) {
54✔
343
            $errorMessage = \implode("\n", [
×
UNCOV
344
                'The file has no valid copyright in header.',
UNCOV
345
                'Expected file header:',
346
                \str_repeat('-', 60),
×
UNCOV
347
                $validHeader,
348
                \str_repeat('-', 60),
×
349
                'Invalid Files (' . \count($invalidFiles) . '):',
×
350
                'See: ' . \implode("\nSee: ", $invalidFiles),
×
UNCOV
351
                '',
UNCOV
352
            ]);
353
            fail($errorMessage);
×
354
        }
355

356
        if ($filesCount > 0) {
54✔
357
            success();
54✔
358
        } else {
359
            success('Files not found');
×
360
        }
361
    }
362
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc