1
1
<?php
2
2
3
- use PHP_CodeSniffer \Files \FileList ;
3
+ use Stringy \Stringy as S ;
4
+ use PHP_CodeSniffer \Runner ;
5
+ use PHP_CodeSniffer \Config ;
4
6
use PHP_CodeSniffer \Reporter ;
7
+ use PHP_CodeSniffer \Files \DummyFile ;
5
8
use PHP_CodeSniffer \Util \Timing ;
6
- use PHP_CodeSniffer \Config ;
7
- use Stringy \Stringy as S ;
8
9
9
- class Runner
10
+ class Executor
10
11
{
11
12
const DEFAULT_EXTENSIONS = array ("php " , "inc " , "module " );
12
13
@@ -23,7 +24,7 @@ public function queueDirectory($dir, $prefix = '')
23
24
{
24
25
chdir ("/code " );
25
26
26
- if (isset ($ this ->config ['include_paths ' ])) {
27
+ if (isset ($ this ->config ['include_paths ' ])) {
27
28
$ this ->queueWithIncludePaths ();
28
29
} else {
29
30
$ this ->queuePaths ($ dir , $ prefix , $ this ->config ['exclude_paths ' ]);
@@ -32,7 +33,8 @@ public function queueDirectory($dir, $prefix = '')
32
33
$ this ->server ->process_work (false );
33
34
}
34
35
35
- public function queueWithIncludePaths () {
36
+ public function queueWithIncludePaths ()
37
+ {
36
38
foreach ($ this ->config ['include_paths ' ] as $ f ) {
37
39
if ($ f !== '. ' and $ f !== '.. ' ) {
38
40
if (is_dir ($ f )) {
@@ -44,7 +46,8 @@ public function queueWithIncludePaths() {
44
46
}
45
47
}
46
48
47
- public function queuePaths ($ dir , $ prefix = '' , $ exclusions = []) {
49
+ public function queuePaths ($ dir , $ prefix = '' , $ exclusions = [])
50
+ {
48
51
$ dir = rtrim ($ dir , '\\/ ' );
49
52
50
53
foreach (scandir ($ dir ) as $ f ) {
@@ -62,7 +65,8 @@ public function queuePaths($dir, $prefix = '', $exclusions = []) {
62
65
}
63
66
}
64
67
65
- public function filterByExtension ($ f , $ prefix = '' ) {
68
+ public function filterByExtension ($ f , $ prefix = '' )
69
+ {
66
70
foreach ($ this ->fileExtensions () as $ file_extension ) {
67
71
if (S::create ($ f )->endsWith ($ file_extension )) {
68
72
$ prefix = ltrim ($ prefix , "\\/ " );
@@ -71,7 +75,8 @@ public function filterByExtension($f, $prefix = '') {
71
75
}
72
76
}
73
77
74
- private function fileExtensions () {
78
+ private function fileExtensions ()
79
+ {
75
80
$ extensions = $ this ->config ['config ' ]['file_extensions ' ];
76
81
77
82
if (empty ($ extensions )) {
@@ -85,55 +90,44 @@ public function run($files)
85
90
{
86
91
try {
87
92
$ resultFile = tempnam (sys_get_temp_dir (), 'phpcodesniffer ' );
93
+ $ config_args = array ( '-s ' , '-p ' );
88
94
89
- $ extra_config_options = array ('--report-json= ' .$ resultFile );
95
+ if (isset ($ this ->config ['config ' ]['ignore_warnings ' ]) && $ this ->config ['config ' ]['ignore_warnings ' ]) {
96
+ $ config_args [] = '-n ' ;
97
+ }
98
+
99
+ Timing::startTiming ();
100
+
101
+ $ runner = new Runner ();
102
+ $ runner ->config = new Config ($ config_args );
90
103
91
104
if (isset ($ this ->config ['config ' ]['standard ' ])) {
92
- $ extra_config_options [] = ' --standard= ' . $ this ->config ['config ' ]['standard ' ];
105
+ $ runner -> config -> standards = explode ( ' , ' , $ this ->config ['config ' ]['standard ' ]) ;
93
106
} else {
94
- $ extra_config_options [] = '--standard=PSR1,PSR2 ' ;
95
- }
96
-
97
- if (isset ($ this ->config ['config ' ]['ignore_warnings ' ]) && $ this ->config ['config ' ]['ignore_warnings ' ]) {
98
- $ extra_config_options [] = '-n ' ;
107
+ $ runner ->config ->standards = array ('PSR1 ' , 'PSR2 ' );
99
108
}
100
109
101
110
if (isset ($ this ->config ['config ' ]['encoding ' ])) {
102
- $ extra_config_options [] = '--encoding= ' . $ this ->config ['config ' ]['encoding ' ];
103
- }
104
-
105
- foreach ($ files as $ file ) {
106
- $ extra_config_options [] = $ file ;
111
+ $ runner ->config ->encoding = $ this ->config ['config ' ]['encoding ' ];
107
112
}
108
113
109
- // prevent any stdout leakage
110
- ob_start ();
111
-
112
- // setup the code sniffer
113
- $ runner = new \PHP_CodeSniffer \Runner ();
114
- $ runner ->config = new Config ($ extra_config_options );
114
+ $ runner ->config ->reports = array ( 'json ' => null );
115
+ $ runner ->config ->reportFile = $ resultFile ;
115
116
$ runner ->init ();
116
117
117
- // setup the code sniffer
118
118
$ runner ->reporter = new Reporter ($ runner ->config );
119
119
120
- // start the code sniffing
121
- Timing::startTiming ();
122
- $ runner ->checkRequirements ();
123
-
124
- $ todo = new FileList ($ runner ->config , $ runner ->ruleset );
125
- foreach ($ todo as $ path => $ file ) {
126
- if ($ file ->ignored === false ) {
127
- $ runner ->processFile ($ file );
128
- }
120
+ foreach ($ files as $ file_path ) {
121
+ $ file = new DummyFile (file_get_contents ($ file_path ), $ runner ->ruleset , $ runner ->config );
122
+ $ file ->path = $ file_path ;
123
+
124
+ $ runner ->processFile ($ file );
129
125
}
130
126
131
- $ runner -> reporter -> printReports ();
127
+ ob_start ();
132
128
133
- // clean up the output buffers (might be more that one)
134
- while (ob_get_level ()) {
135
- ob_end_clean ();
136
- }
129
+ $ runner ->reporter ->printReports ();
130
+ $ report = ob_get_clean ();
137
131
138
132
return $ resultFile ;
139
133
} catch (\Throwable $ e ) {
0 commit comments