Skip to content

Commit f70a5a7

Browse files
authored
Merge pull request #29 from shochdoerfer/feature/phpstan_cache
Improve code generation for factories & proxies
2 parents 76410fc + 88e53f8 commit f70a5a7

File tree

14 files changed

+648
-157
lines changed

14 files changed

+648
-157
lines changed

README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ You can add `bitexpert/phpstan-magento` as a dev dependency, as follows:
1212
composer.phar require --dev bitexpert/phpstan-magento
1313
```
1414

15-
Include extension.neon in your project's PHPStan config:
15+
Include extension.neon and the autoloader.php file in your project's PHPStan config:
1616

17-
```
17+
```neon
18+
parameters:
19+
autoload_files:
20+
- vendor/bitexpert/phpstan-magento/autoload.php
1821
includes:
1922
- vendor/bitexpert/phpstan-magento/extension.neon
2023
```
@@ -25,22 +28,6 @@ includes:
2528
3. A type extension was added so that `ObjectManager` calls return the correct return type.
2629
4. For some classes like the `DataObject` or the `SessionManager` logic was added to be able to support magic method calls.
2730

28-
## Known Issues
29-
30-
Below is a list of known issues when using this extension:
31-
32-
### PHPStan shim does not generate factories, proxies, etc.
33-
34-
This is because the PHPStan shim is included as a phar archive and does therefore not support overriding certain methods in it's namespace. The current known fix for this is to manually load the autoloader that comes with this extension.
35-
36-
Include the autoload.php file in the `autoload_files`-section of your `phpstan.neon`:
37-
38-
```neon
39-
parameters:
40-
autoload_files:
41-
- vendor/bitexpert/phpstan-magento/autoload.php
42-
```
43-
4431
## Contribute
4532

4633
Please feel free to fork and extend existing or add new features and send a pull request with your changes! To establish a consistent code quality, please provide unit tests for all your changes and adapt the documentation.

autoload.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,53 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
// Include this file if you are using the phar version of PHPStan. Since the phar version makes use of dynamic namespaces
13-
// the hack in registration.php to overload spl_autoload_register() and spl_autoload_unregister() does not work any more.
14-
\bitExpert\PHPStan\Magento\Autoload\Autoloader::register();
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 Nette\Neon\Neon;
17+
use PHPStan\Cache\Cache;
18+
19+
// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
20+
(function (array $argv = []) {
21+
// Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the
22+
// location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition!
23+
$configFile = '';
24+
if (count($argv) > 0) {
25+
foreach($argv as $idx => $value) {
26+
if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) {
27+
$configFile = $argv[$idx + 1];
28+
break;
29+
}
30+
}
31+
}
32+
33+
if (empty($configFile)) {
34+
$currentWorkingDirectory = getcwd();
35+
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
36+
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
37+
if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) {
38+
$configFile = $discoverableConfigFile;
39+
break;
40+
}
41+
}
42+
}
43+
44+
$tmpDir = sys_get_temp_dir() . '/phpstan';
45+
if (!empty($configFile)) {
46+
$neonConfig = Neon::decode(file_get_contents($configFile));
47+
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
48+
$tmpDir = $neonConfig['parameters']['tmpDir'];
49+
}
50+
}
51+
52+
$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));
53+
54+
$mockAutoloader = new MockAutoloader();
55+
$factoryAutoloader = new FactoryAutoloader($cache);
56+
$proxyAutoloader = new ProxyAutoloader($cache);
57+
58+
\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
59+
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
60+
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
61+
})($GLOBALS['argv'] ?? []);

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@
1717
],
1818
"require": {
1919
"php": "^7.2.0",
20+
"nette/neon": "^3.1",
2021
"phpstan/phpstan": "^0.12.18"
2122
},
2223
"require-dev": {
24+
"bitexpert/phing-securitychecker": "^0.4.0",
2325
"captainhook/captainhook": "^5.1.2",
2426
"captainhook/plugin-composer": "^5.1.3",
27+
"mikey179/vfsstream": "^1.6",
2528
"nikic/php-parser": "^4.3",
26-
"phpunit/phpunit": "^8.5.3",
27-
"squizlabs/php_codesniffer": "^3.4",
2829
"phing/phing": "^2.16",
29-
"bitexpert/phing-securitychecker": "^0.4.0"
30+
"phpunit/phpunit": "^8.5.3",
31+
"squizlabs/php_codesniffer": "^3.4"
3032
},
3133
"autoload": {
32-
"files": [
33-
"registration.php"
34-
],
3534
"psr-4": {
3635
"bitExpert\\PHPStan\\": "src/bitExpert/PHPStan"
3736
}

composer.lock

Lines changed: 109 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

registration.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)