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
|
|
|
namespace LIN3S\CS\Checker; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Beñat Espiña <[email protected]> |
16
|
|
|
* @author Jon Torrado <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class PhpCsFixer implements Checker |
19
|
|
|
{ |
20
|
|
|
use FileFinder; |
21
|
|
|
|
22
|
|
|
public static function check(array $files = [], array $parameters = null) |
23
|
|
|
{ |
24
|
|
|
foreach ($files as $file) { |
25
|
|
|
if (false === self::exist($file, $parameters['phpcsfixer_path'], 'php')) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Exec PHP function is used because php-cs-fixer uses Symfony Process component inside |
30
|
|
|
// ProcessBuilder fails when is launched from another ProcessBuilder |
31
|
|
|
$commandLine = [ |
32
|
|
|
'php', |
33
|
|
|
'vendor/friendsofphp/php-cs-fixer/php-cs-fixer', |
34
|
|
|
'fix', |
35
|
|
|
$file, |
36
|
|
|
'--config=.php_cs', |
37
|
|
|
]; |
38
|
|
|
exec(implode(' ', $commandLine)); |
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 phpCsConfigFile(array $parameters) |
49
|
|
|
{ |
50
|
|
|
self::configFile('.php_cs', $parameters); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private static function phpspecCsConfigFile(array $parameters) |
54
|
|
|
{ |
55
|
|
|
self::configFile('.phpspec_cs', $parameters); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static function configFile($fileName, array $parameters) |
59
|
|
|
{ |
60
|
|
|
$file = file_get_contents(__DIR__ . '/src/' . $fileName . '.dist'); |
61
|
|
|
|
62
|
|
|
$file = str_replace( |
63
|
|
|
'$$CHANGE-FOR-YOUR-AWESOME-NAME CHANGE-FOR-TYPE$$', |
64
|
|
|
$parameters['name'], |
65
|
|
|
$file |
66
|
|
|
); |
67
|
|
|
$file = str_replace( |
68
|
|
|
'$$CHANGE-FOR-YEAR$$', |
69
|
|
|
$parameters['year'], |
70
|
|
|
$file |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
file_put_contents(self::location($parameters) . '/' . $fileName, $file); |
75
|
|
|
} catch (\Exception $exception) { |
76
|
|
|
echo sprintf("Something wrong happens during the creating process: \n%s\n", $exception->getMessage()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private static function location(array $parameters) |
81
|
|
|
{ |
82
|
|
|
return $parameters['root_directory'] . '/' . $parameters['phpcsfixer_file_location']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|