1616class StanCs
1717{
1818 /**
19- * Tam kde je složka vendor.
19+ * location of analysed project (directory containing vendor directory) .
2020 *
2121 * @var string
2222 */
@@ -37,12 +37,26 @@ class StanCs
3737 */
3838 private $ config ;
3939
40+ /**
41+ * @var string
42+ */
43+ private $ fileToAnalise ;
44+
45+
46+
47+ /**
48+ * StanCs constructor.
49+ *
50+ * @param array $argv
51+ * @param string $projectRootDir
52+ */
4053 public function __construct (array $ argv , string $ projectRootDir )
4154 {
42- $ this ->projectRootDir = $ projectRootDir ;
55+ $ this ->projectRootDir = $ projectRootDir ;
4356 $ this ->phpstancsRootDir = __DIR__ . '/../ ' ;
44- $ this ->argv = $ argv ;
45- $ this ->config = $ this ->getConfig ();
57+ $ this ->argv = $ argv ;
58+ $ this ->fileToAnalise = $ this ->argv [1 ];
59+ $ this ->config = $ this ->getConfig ();
4660 }
4761
4862 public function run (): string
@@ -70,23 +84,30 @@ public function run(): string
7084 */
7185 protected function getStanOutput (): string
7286 {
73- // zajistíme, aby phpstan našel autoloader i když je umístěn mimo projekt
74- // PHP satan primárně načítá autoloader z cwd/vendor/autoloader.php
87+ $ cwdBak = getcwd ();
88+ if ($ cwdBak === false ) {
89+ throw new RuntimeException ("getcwd() failed " );
90+ }
91+ // PHP stan load autoloader primary from `cwd`/vendor/autoloader.php
7592 exec ('cd ' . escapeshellarg ($ this ->projectRootDir ));
7693
94+ $ configLocation =
95+ file_exists ("{$ this ->projectRootDir }phpstan.neon " ) ? $ this ->projectRootDir : $ this ->phpstancsRootDir ;
96+
7797 ob_start ();
7898 passthru (
79- "{$ this ->projectRootDir } vendor/bin/ phpstan " .
80- " analyse {$ this ->argv [ 1 ] }" .
81- " -c {$ this -> projectRootDir }phpstan.neon " .
99+ "{$ this ->getBinDir ()} phpstan " .
100+ " analyse {$ this ->fileToAnalise }" .
101+ " -c {$ configLocation }phpstan.neon " .
82102 ' --error-format cslike --no-progress '
83103 );
84104
85105 $ output = ob_get_clean ();
106+
86107 if ($ output === false ) {
87108 throw new RuntimeException ('ob_get_clean failed ' );
88109 }
89-
110+ exec ( ' cd ' . escapeshellarg ( $ cwdBak ));
90111 return $ output ;
91112 }
92113
@@ -99,7 +120,7 @@ protected function getCsOutput(): string
99120 array_shift ($ args );
100121
101122 ob_start ();
102- passthru ("{$ this ->projectRootDir } vendor/bin/ phpcs " . implode (' ' , $ args ));
123+ passthru ("{$ this ->getBinDir ()} phpcs " . implode (' ' , $ args ));
103124
104125 $ output = ob_get_clean ();
105126 if ($ output === false ) {
@@ -139,6 +160,12 @@ private function getConfig(): Config
139160
140161 return $ config ;
141162 }
163+
164+ protected function getBinDir (): string
165+ {
166+ return file_exists ("{$ this ->phpstancsRootDir }vendor/bin/ " ) ?
167+ "{$ this ->phpstancsRootDir }vendor/bin/ " : "{$ this ->phpstancsRootDir }/../../bin/ " ;
168+ }
142169}
143170
144171// StanCs.php End
0 commit comments