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

Commit 8cf1f68

Browse files
authored
Merge pull request #9 from /issues/8
Implemented using autoloader from the project
2 parents f3b2f9d + 8b05a76 commit 8cf1f68

File tree

10 files changed

+147
-39
lines changed

10 files changed

+147
-39
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
parameters:
22
excludes_analyse:
33
- %currentWorkingDirectory%/tests/fixtures/*
4+
ignoreErrors:
5+
- '~^Class Dummy not found.$~i'

src/Application.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public function run(Command $command, array $args) : int
4444
return 1;
4545
}
4646

47-
$config = (new ConfigLoader())->loadConfig(getcwd());
47+
$loader = new ProjectLoader(getcwd());
48+
$loader->registerClassLoader();
49+
50+
$config = $loader->getPhpCodeSnifferConfiguration();
4851

4952
try {
5053
if ($config !== null) {
Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,88 @@
77
/**
88
* Configuration loader
99
*/
10-
class ConfigLoader
10+
class ProjectLoader
1111
{
1212
/**
13-
* @param string $dir Current directory
14-
* @return array|null Configuration parameters or NULL if not found
13+
* Project root
14+
*
15+
* @var string|null
16+
*/
17+
private $root;
18+
19+
/**
20+
* Indicates whether Composer configuration was loaded
21+
*
22+
* @var bool
23+
*/
24+
private $composerConfigurationLoaded = false;
25+
26+
/**
27+
* Composer vendor directory
28+
*
29+
* @var string
1530
*/
16-
public function loadConfig($dir) : ?array
31+
private $vendorDirectory = 'vendor';
32+
33+
/**
34+
* @param string $directory Current working directory
35+
*/
36+
public function __construct(string $directory)
1737
{
18-
$path = $this->findComposerConfig($dir);
38+
$this->root = $this->findProjectRoot($directory);
39+
}
1940

20-
if ($path === null) {
41+
/**
42+
* @return array|null Configuration parameters or NULL if not found
43+
*/
44+
public function getPhpCodeSnifferConfiguration() : ?array
45+
{
46+
if ($this->root === null) {
2147
return null;
2248
}
2349

24-
$composerConfig = $this->loadComposerConfig($path);
25-
$vendorDir = $composerConfig['vendor-dir'] ?? 'vendor';
26-
27-
$path = sprintf('%s/%s/squizlabs/php_codesniffer/CodeSniffer.conf', dirname($path), $vendorDir);
50+
$path = sprintf('%s/%s/squizlabs/php_codesniffer/CodeSniffer.conf', $this->root, $this->getVendorDirectory());
2851

2952
if (!file_exists($path)) {
3053
return null;
3154
}
3255

33-
$config = $this->loadPhpCodeSnifferConfig($path);
56+
$config = $this->loadPhpCodeSnifferConfiguration($path);
3457

3558
return $this->massageConfig($config, $path);
3659
}
3760

3861
/**
39-
* Finds path of composer.json located in the closest common parent directory
62+
* Registers class loader if it's defined in the project
63+
*/
64+
public function registerClassLoader() : void
65+
{
66+
if ($this->root === null) {
67+
return;
68+
}
69+
70+
$path = sprintf('%s/%s/autoload.php', $this->root, $this->getVendorDirectory());
71+
72+
if (!is_file($path)) {
73+
return;
74+
}
75+
76+
require $path;
77+
}
78+
79+
/**
80+
* Finds project root by locating composer.json located in the current working directory or its closest parent
4081
*
41-
* @param string $dir Current directory
82+
* @param string $dir Current working directory
4283
* @return string|null
4384
*/
44-
private function findComposerConfig(string $dir) : ?string
85+
private function findProjectRoot(string $dir) : ?string
4586
{
4687
do {
4788
$path = $dir . '/composer.json';
4889

4990
if (file_exists($path)) {
50-
return $path;
91+
return $dir;
5192
}
5293

5394
$prev = $dir;
@@ -57,15 +98,29 @@ private function findComposerConfig(string $dir) : ?string
5798
return null;
5899
}
59100

101+
private function getVendorDirectory() : string
102+
{
103+
if (!$this->composerConfigurationLoaded) {
104+
$config = $this->loadComposerConfiguration();
105+
106+
if (isset($config['vendor-dir'])) {
107+
$this->vendorDirectory = $config['vendor-dir'];
108+
}
109+
110+
$this->composerConfigurationLoaded = true;
111+
}
112+
113+
return $this->vendorDirectory;
114+
}
115+
60116
/**
61117
* Loads composer configuration from the given file
62118
*
63-
* @param string $path File path
64119
* @return array Configuration parameters
65120
*/
66-
private function loadComposerConfig(string $path) : array
121+
private function loadComposerConfiguration() : array
67122
{
68-
$contents = file_get_contents($path);
123+
$contents = file_get_contents($this->root . '/composer.json');
69124
$config = json_decode($contents, true);
70125

71126
if (json_last_error() !== JSON_ERROR_NONE) {
@@ -81,7 +136,7 @@ private function loadComposerConfig(string $path) : array
81136
* @param string $path File path
82137
* @return array Configuration parameters
83138
*/
84-
private function loadPhpCodeSnifferConfig(string $path) : array
139+
private function loadPhpCodeSnifferConfiguration(string $path) : array
85140
{
86141
$phpCodeSnifferConfig = [];
87142

tests/ApplicationTest.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use function chdir;
66
use DiffSniffer\Application;
77
use DiffSniffer\Command;
8+
use Dummy;
89
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011
use const DIRECTORY_SEPARATOR;
@@ -17,7 +18,27 @@ class ApplicationTest extends TestCase
1718
*/
1819
public function testUseCase(string $useCase, int $expectedExitCode)
1920
{
20-
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . $useCase;
21+
$app = new Application();
22+
23+
$this->expectOutputString($this->getExpectedOutput($useCase));
24+
$exitCode = $app->run($this->createCommand($useCase), [__FILE__]);
25+
26+
$this->assertSame($expectedExitCode, $exitCode);
27+
}
28+
29+
public function testClassLoader()
30+
{
31+
$app = new Application();
32+
33+
$app->run($this->createCommand('class-loader'), [__FILE__]);
34+
35+
self::assertTrue(class_exists(Dummy::class));
36+
}
37+
38+
private function createCommand(string $useCase) : Command
39+
{
40+
$dir = $this->getDirectory($useCase);
41+
2142
$changeset = new FixtureChangeset($dir);
2243
chdir($dir . DIRECTORY_SEPARATOR . 'tree');
2344

@@ -26,15 +47,23 @@ public function testUseCase(string $useCase, int $expectedExitCode)
2647
$command->expects($this->once())
2748
->method('createChangeset')
2849
->willReturn($changeset);
29-
$app = new Application();
3050

31-
$expectedOutput = file_get_contents($dir . DIRECTORY_SEPARATOR . 'output.txt');
32-
$expectedOutput = str_replace("\n", PHP_EOL, $expectedOutput);
51+
return $command;
52+
}
3353

34-
$this->expectOutputString($expectedOutput);
35-
$exitCode = $app->run($command, [__FILE__]);
54+
private function getExpectedOutput(string $useCase) : string
55+
{
56+
$dir = $this->getDirectory($useCase);
3657

37-
$this->assertSame($expectedExitCode, $exitCode);
58+
$output = file_get_contents($dir . DIRECTORY_SEPARATOR . 'output.txt');
59+
$output = str_replace("\n", PHP_EOL, $output);
60+
61+
return $output;
62+
}
63+
64+
private function getDirectory(string $useCase) : string
65+
{
66+
return __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . $useCase;
3867
}
3968

4069
public static function useCaseProvider()
@@ -52,6 +81,10 @@ public static function useCaseProvider()
5281
'exclude-pattern',
5382
1,
5483
],
84+
'class-loader' => [
85+
'class-loader',
86+
0,
87+
],
5588
];
5689
}
5790
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22

33
namespace DiffSniffer\Tests;
44

5-
use DiffSniffer\ConfigLoader;
5+
use DiffSniffer\ProjectLoader;
66
use DiffSniffer\Exception;
77
use PHPUnit\Framework\TestCase;
88
use VirtualFileSystem\FileSystem;
99

1010
/**
11-
* @covers \DiffSniffer\ConfigLoader
11+
* @covers \DiffSniffer\ProjectLoader
1212
*/
13-
class ConfigLoaderTest extends TestCase
13+
class ProjectLoaderTest extends TestCase
1414
{
1515
/**
1616
* @var FileSystem
1717
*/
1818
private $fs;
1919

20-
/**
21-
* @var ConfigLoader
22-
*/
23-
private $loader;
24-
2520
protected function setUp() : void
2621
{
2722
parent::setUp();
2823

2924
$this->fs = new FileSystem();
30-
$this->loader = new ConfigLoader();
3125
}
3226

3327
/**
@@ -38,7 +32,9 @@ public function success(array $structure, string $dir, array $expected)
3832
{
3933
$this->fs->createStructure($structure);
4034
$dir = $this->fs->path($dir);
41-
$config = $this->loader->loadConfig($dir);
35+
36+
$loader = new ProjectLoader($dir);
37+
$config = $loader->getPhpCodeSnifferConfiguration();
4238

4339
if (isset($expected['installed_paths'])) {
4440
$expected['installed_paths'] = implode(',', array_map(function ($path) {
@@ -114,7 +110,9 @@ public function notFound(array $structure, string $dir)
114110
{
115111
$this->fs->createStructure($structure);
116112
$dir = $this->fs->path($dir);
117-
$config = $this->loader->loadConfig($dir);
113+
114+
$loader = new ProjectLoader($dir);
115+
$config = $loader->getPhpCodeSnifferConfiguration();
118116

119117
$this->assertNull($config);
120118
}
@@ -149,8 +147,10 @@ public function failure(array $structure, string $dir)
149147
$this->fs->createStructure($structure);
150148
$dir = $this->fs->path($dir);
151149

150+
$loader = new ProjectLoader($dir);
151+
152152
$this->expectException(Exception::class);
153-
$this->loader->loadConfig($dir);
153+
$loader->getPhpCodeSnifferConfiguration();
154154
}
155155

156156
public static function failureProvider() : array

tests/fixtures/class-loader/changeset.diff

Whitespace-only changes.

tests/fixtures/class-loader/output.txt

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Dummy
4+
{
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"vendor-dir": "dependencies"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
spl_autoload_register(function (string $class) : void {
4+
if ($class === Dummy::class) {
5+
require __DIR__ . '/../Dummy.php';
6+
}
7+
});

0 commit comments

Comments
 (0)