-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolymorphine-phpcs
More file actions
101 lines (85 loc) · 3.23 KB
/
Copy pathpolymorphine-phpcs
File metadata and controls
101 lines (85 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env php
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Dev package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* PHP_CodeSniffer detects violations of a defined coding standard.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/HEAD/licence.txt BSD Licence
*/
$scriptRoot = getcwd() === __DIR__ ? __DIR__ : dirname(__DIR__, 3);
require_once PhpCSScript::path($scriptRoot, 'vendor/squizlabs/php_codesniffer/autoload.php');
require_once PhpCSScript::path($scriptRoot, 'vendor/autoload.php');
$codeSniffer = new PhpCSScript(new PHP_CodeSniffer\Runner(), $scriptRoot === __DIR__);
// Checks for $argv[1] are workarounds for PhpStorm inspection that either sends file content
// through STDIN ($argv[1] = '-') or checks for version number ($argv[1] = '--version')
$commandType = $argv[1] ?? null;
switch ($commandType) {
case '--version':
$exitCode = $codeSniffer->versionCheck($argv);
break;
case '-':
$exitCode = $codeSniffer->fileInspection($argv);
break;
default:
$exitCode = $commandType ? $codeSniffer->fileCheck($argv) : $codeSniffer->projectScan();
$exitCode = $exitCode ?: '--- Done (OK) ---';
}
exit($exitCode);
class PhpCSScript
{
public static function path(string $root, string $path): string
{
return $root . DIRECTORY_SEPARATOR . str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
}
private PHP_CodeSniffer\Runner $runner;
private bool $selfCheck;
public function __construct(PHP_CodeSniffer\Runner $runner, bool $selfCheck)
{
$this->runner = $runner;
$this->selfCheck = $selfCheck;
}
public function projectScan(): int
{
$this->selfCheck || $this->includeProjectAutoload(getcwd());
return $this->runScript(['--version']) | $this->runScript([$this->standard(), 'src', 'tests']);
}
public function versionCheck(array $args): int
{
return $this->runScript($args);
}
public function fileInspection(array $args): int
{
// PhpStorm inspection should provide project root directory instead of redundant ruleset path option,
// so that it could be used to locate project's Composer autoload.php file.
$this->selfCheck || $this->includeProjectAutoload(explode('=', $args[2])[1]);
$args[2] = $this->standard();
return $this->runScript($args);
}
public function fileCheck(array $args): int
{
$this->selfCheck || $this->includeProjectAutoload(getcwd());
return $this->runScript([$this->standard(), $args[1]]);
}
private function runScript(array $args): int
{
$_SERVER['argv'] = [__FILE__, ...$args];
return $this->runner->runPHPCS();
}
private function standard(): string
{
return '--standard=' . self::path(__DIR__, 'phpcs.xml');
}
private function includeProjectAutoload(string $projectRoot): void
{
require_once self::path($projectRoot, 'vendor/autoload.php');
}
}