Skip to content

Commit 05b71a3

Browse files
authored
Merge pull request #163 from lacrossefootwear/bugfix/161
Determine tmpDir path relative to config file when appropriate
2 parents 923a30f + cde39ac commit 05b71a3

File tree

9 files changed

+173
-53
lines changed

9 files changed

+173
-53
lines changed

autoload.php

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,24 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
use bitExpert\PHPStan\Magento\Autoload\Cache\FileCacheStorage;
13-
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
14-
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
15-
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
16-
use bitExpert\PHPStan\Magento\Autoload\TestFrameworkAutoloader;
17-
use Nette\Neon\Neon;
18-
use PHPStan\Cache\Cache;
19-
2012
// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
21-
(function (array $argv = []) {
22-
// Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the
23-
// location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition!
24-
$configFile = '';
25-
if (count($argv) > 0) {
26-
foreach($argv as $idx => $value) {
27-
if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) {
28-
$configFile = $argv[$idx + 1];
29-
break;
30-
}
31-
}
32-
}
33-
34-
if (empty($configFile)) {
35-
$currentWorkingDirectory = getcwd();
36-
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
37-
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
38-
if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) {
39-
$configFile = $discoverableConfigFile;
40-
break;
41-
}
42-
}
43-
}
4413

45-
$tmpDir = sys_get_temp_dir() . '/phpstan';
46-
if (!empty($configFile)) {
47-
$neonConfig = Neon::decode(file_get_contents($configFile));
48-
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
49-
$tmpDir = $neonConfig['parameters']['tmpDir'];
50-
}
14+
use bitExpert\PHPStan\Magento\Autoload\Autoloader;
15+
use PHPStan\DependencyInjection\Container;
16+
17+
if (!isset($container) || !$container instanceof Container) {
18+
echo 'No container found, or container not of expected type' . PHP_EOL;
19+
die(-1);
20+
}
21+
22+
foreach ($container->getServicesByTag('phpstan.magento.autoloader') as $autoloader) {
23+
/** @var Autoloader|object $autoloader */
24+
if (!$autoloader instanceof Autoloader) {
25+
echo 'Services tagged with \'phpstan.magento.autoloader\' must extend ' .
26+
'bitExpert\PHPStan\Magento\Autoload\Autoloader!' . PHP_EOL .
27+
get_class($autoloader) . ' does not.' . PHP_EOL;
28+
die(-1);
5129
}
5230

53-
$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));
54-
55-
$mockAutoloader = new MockAutoloader();
56-
$testFrameworkAutoloader = new TestFrameworkAutoloader();
57-
$factoryAutoloader = new FactoryAutoloader($cache);
58-
$proxyAutoloader = new ProxyAutoloader($cache);
59-
60-
\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
61-
\spl_autoload_register([$testFrameworkAutoloader, 'autoload'], true, false);
62-
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
63-
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
64-
})($GLOBALS['argv'] ?? []);
31+
$autoloader->register();
32+
}

extension.neon

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ services:
4040
class: bitExpert\PHPStan\Magento\Rules\AbstractModelRetrieveCollectionViaFactoryRule
4141
-
4242
class: bitExpert\PHPStan\Magento\Rules\AbstractModelUseServiceContractRule
43+
fileCacheStorage:
44+
class: bitExpert\PHPStan\Magento\Autoload\Cache\FileCacheStorage
45+
arguments:
46+
directory: %tmpDir%/cache/PHPStan
47+
autoloaderCache:
48+
autowired: false
49+
class: PHPStan\Cache\Cache
50+
arguments:
51+
storage: @fileCacheStorage
52+
mockAutoloader:
53+
class: bitExpert\PHPStan\Magento\Autoload\MockAutoloader
54+
tags:
55+
- phpstan.magento.autoloader
56+
testFrameworkAutoloader:
57+
class: bitExpert\PHPStan\Magento\Autoload\TestFrameworkAutoloader
58+
tags:
59+
- phpstan.magento.autoloader
60+
factoryAutoloader:
61+
class: bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader
62+
arguments:
63+
cache: @autoloaderCache
64+
tags:
65+
- phpstan.magento.autoloader
66+
proxyAutoloader:
67+
class: bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader
68+
arguments:
69+
cache: @autoloaderCache
70+
tags:
71+
- phpstan.magento.autoloader

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ parameters:
3434
-
3535
message: '~is not covered by backward compatibility promise.~'
3636
path: tests/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloaderUnitTest.php
37+
-
38+
message: '~is not covered by backward compatibility promise.~'
39+
path: tests/bitExpert/PHPStan/Magento/Autoload/RegistrationUnitTest.php
3740
-
3841
message: '~bitExpert\\PHPStan\\Magento\\Rules\\Helper\\SampleModel::__construct\(\) does not call parent constructor~'
3942
path: tests/bitExpert/PHPStan/Magento/Rules/Helper/SampleModel.php
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
interface Autoloader
16+
{
17+
/**
18+
* Begin autoloading
19+
*/
20+
public function register(): void;
21+
22+
/**
23+
* Stop autoloading
24+
*/
25+
public function unregister(): void;
26+
}

src/bitExpert/PHPStan/Magento/Autoload/FactoryAutoloader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use PHPStan\Cache\Cache;
1616

17-
class FactoryAutoloader
17+
class FactoryAutoloader implements Autoloader
1818
{
1919
/**
2020
* @var Cache
@@ -94,4 +94,14 @@ protected function getFileContents(string $class): string
9494
$template
9595
);
9696
}
97+
98+
public function register(): void
99+
{
100+
\spl_autoload_register([$this, 'autoload'], true, false);
101+
}
102+
103+
public function unregister(): void
104+
{
105+
spl_autoload_unregister([$this, 'autoload']);
106+
}
97107
}

src/bitExpert/PHPStan/Magento/Autoload/MockAutoloader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* The MockAutoloader is responsible to load custom mocked classes or interfaces instead of the original Magento classes
1717
* or interfaces. This is needed as not all interfaces expose all public methods that can be called on those objects.
1818
*/
19-
class MockAutoloader
19+
class MockAutoloader implements Autoloader
2020
{
2121
public function autoload(string $class): void
2222
{
@@ -25,4 +25,14 @@ public function autoload(string $class): void
2525
include($filename);
2626
}
2727
}
28+
29+
public function register(): void
30+
{
31+
\spl_autoload_register([$this, 'autoload'], true, true);
32+
}
33+
34+
public function unregister(): void
35+
{
36+
\spl_autoload_unregister([$this, 'autoload']);
37+
}
2838
}

src/bitExpert/PHPStan/Magento/Autoload/ProxyAutoloader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use PHPStan\Cache\Cache;
1616

17-
class ProxyAutoloader
17+
class ProxyAutoloader implements Autoloader
1818
{
1919
/**
2020
* @var Cache
@@ -158,4 +158,14 @@ protected function getFileContents(string $class): string
158158
$template
159159
);
160160
}
161+
162+
public function register(): void
163+
{
164+
\spl_autoload_register([$this, 'autoload'], true, false);
165+
}
166+
167+
public function unregister(): void
168+
{
169+
\spl_autoload_unregister([$this, 'autoload']);
170+
}
161171
}

src/bitExpert/PHPStan/Magento/Autoload/TestFrameworkAutoloader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Autoloader for Magento\TestFramework classes as those are not loaded by Composer by default which makes PHPStan
1717
* not know about them.
1818
*/
19-
class TestFrameworkAutoloader
19+
class TestFrameworkAutoloader implements Autoloader
2020
{
2121
public function autoload(string $class): void
2222
{
@@ -42,4 +42,14 @@ public function autoload(string $class): void
4242
}
4343
}
4444
}
45+
46+
public function register(): void
47+
{
48+
\spl_autoload_register([$this, 'autoload'], true, false);
49+
}
50+
51+
public function unregister(): void
52+
{
53+
\spl_autoload_unregister([$this, 'autoload']);
54+
}
4555
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Autoload;
14+
15+
use PHPStan\Cache\Cache;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class RegistrationUnitTest extends TestCase
19+
{
20+
/**
21+
* @test
22+
* @dataProvider provideAutoloaders()
23+
*/
24+
public function autoloadersCanRegisterAndUnregister(Autoloader $autoloader): void
25+
{
26+
/** @var array<callable> $initialAutoloadFunctions */
27+
$initialAutoloadFunctions = spl_autoload_functions();
28+
29+
$autoloader->register();
30+
/** @var array<callable> $registerAutoloadFunctions */
31+
$registerAutoloadFunctions = spl_autoload_functions();
32+
static::assertCount(count($initialAutoloadFunctions) + 1, $registerAutoloadFunctions);
33+
34+
$autoloader->unregister();
35+
/** @var array<callable> $unregisterAutoloadFunctions */
36+
$unregisterAutoloadFunctions = spl_autoload_functions();
37+
static::assertCount(count($initialAutoloadFunctions), $unregisterAutoloadFunctions);
38+
}
39+
40+
/**
41+
* @return array<array<Autoloader>>
42+
*/
43+
public function provideAutoloaders(): array
44+
{
45+
$cache = new Cache($this->getMockBuilder(\PHPStan\Cache\CacheStorage::class)->getMock());
46+
47+
return [
48+
[new FactoryAutoloader($cache)],
49+
[new MockAutoloader()],
50+
[new ProxyAutoloader($cache)],
51+
[new TestFrameworkAutoloader()]
52+
];
53+
}
54+
}

0 commit comments

Comments
 (0)