Skip to content
This repository was archived by the owner on Nov 23, 2020. It is now read-only.

Commit d58c9e8

Browse files
authored
Merge pull request #8 from matlad/win_global_support
Win global support
2 parents 1f579d2 + 5349d69 commit d58c9e8

4 files changed

Lines changed: 57 additions & 13 deletions

File tree

bin/phpcs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33

44
use matla\phpstancs\StanCs;
55

6+
67
$vendorDir = is_dir(__DIR__ . '/../../../../vendor/')
78
? __DIR__ . '/../../../../vendor/'
89
: __DIR__ . '/../vendor/';
910

10-
require $vendorDir . 'autoload.php';
11-
11+
require_once $vendorDir . 'autoload.php';
1212
$projectRoot = $vendorDir . '../';
1313

14+
$autoloaderInWorkingDirectory = getcwd() . '/vendor/autoload.php';
15+
if (is_file($autoloaderInWorkingDirectory)) {
16+
$projectRoot = getcwd() . '/';
17+
}
18+
19+
1420
if (!is_dir($projectRoot)) {
1521
throw new LogicException('Project root not found');
1622
}
File renamed without changes.

src/StanCs.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class 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

tests/BuggiClass.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace matla\phpstancs;
4+
5+
class BuggiClass
6+
{
7+
/**
8+
* @var array<int>
9+
*/
10+
protected $stringArray = ["not Int"];
11+
}

0 commit comments

Comments
 (0)