1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CS library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\CS\Checker; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Beñat Espiña <[email protected]> |
18
|
|
|
* @author Jon Torrado <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class PhpCsFixer implements Checker |
21
|
|
|
{ |
22
|
|
|
use FileFinder; |
23
|
|
|
|
24
|
|
|
public static function check(array $files = [], array $parameters = null) |
25
|
|
|
{ |
26
|
|
|
foreach ($files as $file) { |
27
|
|
|
if (false === self::exist($file, $parameters['phpcsfixer_path'], 'php') |
28
|
|
|
&& false === self::exist($file, $parameters['phpcsfixer_test_path'], 'php') |
29
|
|
|
) { |
30
|
|
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (mb_strpos($file, 'Spec') !== false) { |
34
|
|
|
self::execute($file, $parameters, '.phpspec_cs'); |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
self::execute($file, $parameters); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function file(array $parameters) |
43
|
|
|
{ |
44
|
|
|
self::phpCsConfigFile($parameters); |
45
|
|
|
self::phpspecCsConfigFile($parameters); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private static function execute($file, array $parameters, $checkFile = '.php_cs') |
49
|
|
|
{ |
50
|
|
|
// Exec PHP function is used because php-cs-fixer uses Symfony Process component inside |
51
|
|
|
// ProcessBuilder fails when is launched from another ProcessBuilder |
52
|
|
|
$commandLine = [ |
53
|
|
|
'php', |
54
|
|
|
'vendor/friendsofphp/php-cs-fixer/php-cs-fixer', |
55
|
|
|
'fix', |
56
|
|
|
$file, |
57
|
|
|
'--config=' . self::location($parameters) . '/' . $checkFile, |
58
|
|
|
]; |
59
|
|
|
exec(implode(' ', $commandLine)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private static function phpCsConfigFile(array $parameters) |
63
|
|
|
{ |
64
|
|
|
self::configFile('.php_cs', $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private static function phpspecCsConfigFile(array $parameters) |
68
|
|
|
{ |
69
|
|
|
self::configFile('.phpspec_cs', $parameters); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private static function configFile($fileName, array $parameters) |
73
|
|
|
{ |
74
|
|
|
$file = file_get_contents(__DIR__ . '/../' . $fileName . '.dist'); |
75
|
|
|
|
76
|
|
|
$file = str_replace( |
77
|
|
|
'$$CHANGE-FOR-YOUR-AWESOME-NAME CHANGE-FOR-TYPE$$', |
78
|
|
|
$parameters['name'], |
79
|
|
|
$file |
80
|
|
|
); |
81
|
|
|
$file = str_replace( |
82
|
|
|
'$$CHANGE-FOR-YEAR$$', |
83
|
|
|
$parameters['year'], |
84
|
|
|
$file |
85
|
|
|
); |
86
|
|
|
$file = str_replace( |
87
|
|
|
'$$CHANGE-FOR-TYPE$$', |
88
|
|
|
$parameters['type'], |
89
|
|
|
$file |
90
|
|
|
); |
91
|
|
|
$file = str_replace( |
92
|
|
|
'$$CHANGE-FOR-PHPCSFIXER-PATH$$', |
93
|
|
|
'/' . $parameters['phpcsfixer_path'], |
94
|
|
|
$file |
95
|
|
|
); |
96
|
|
|
$file = str_replace( |
97
|
|
|
'$$CHANGE-FOR-PHPCSFIXER-TEST-PATH$$', |
98
|
|
|
'/' . $parameters['phpcsfixer_test_path'], |
99
|
|
|
$file |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
file_put_contents(self::location($parameters) . '/' . $fileName, $file); |
104
|
|
|
} catch (\Exception $exception) { |
105
|
|
|
echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private static function location(array $parameters) |
110
|
|
|
{ |
111
|
|
|
return $parameters['root_directory'] . '/' . $parameters['phpcsfixer_file_location']; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|