Skip to content

Commit

Permalink
Merge pull request #29 from shochdoerfer/feature/phpstan_cache
Browse files Browse the repository at this point in the history
Improve code generation for factories & proxies
  • Loading branch information
shochdoerfer committed Apr 18, 2020
2 parents 76410fc + 88e53f8 commit f70a5a7
Show file tree
Hide file tree
Showing 14 changed files with 648 additions and 157 deletions.
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ You can add `bitexpert/phpstan-magento` as a dev dependency, as follows:
composer.phar require --dev bitexpert/phpstan-magento
```

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

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

## Known Issues

Below is a list of known issues when using this extension:

### PHPStan shim does not generate factories, proxies, etc.

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.

Include the autoload.php file in the `autoload_files`-section of your `phpstan.neon`:

```neon
parameters:
autoload_files:
- vendor/bitexpert/phpstan-magento/autoload.php
```

## Contribute

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.
Expand Down
53 changes: 50 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,53 @@
* file that was distributed with this source code.
*/

// Include this file if you are using the phar version of PHPStan. Since the phar version makes use of dynamic namespaces
// the hack in registration.php to overload spl_autoload_register() and spl_autoload_unregister() does not work any more.
\bitExpert\PHPStan\Magento\Autoload\Autoloader::register();
use bitExpert\PHPStan\Magento\Autoload\Cache\FileCacheStorage;
use bitExpert\PHPStan\Magento\Autoload\FactoryAutoloader;
use bitExpert\PHPStan\Magento\Autoload\MockAutoloader;
use bitExpert\PHPStan\Magento\Autoload\ProxyAutoloader;
use Nette\Neon\Neon;
use PHPStan\Cache\Cache;

// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
(function (array $argv = []) {
// Sadly we don't have access to the parsed phpstan.neon configuration at this point we need to look up the
// location of the config file and parse it with the Neon parser to be able to extract the tmpDir definition!
$configFile = '';
if (count($argv) > 0) {
foreach($argv as $idx => $value) {
if ((strtolower($value) === '-c') && isset($argv[$idx + 1])) {
$configFile = $argv[$idx + 1];
break;
}
}
}

if (empty($configFile)) {
$currentWorkingDirectory = getcwd();
foreach (['phpstan.neon', 'phpstan.neon.dist'] as $discoverableConfigName) {
$discoverableConfigFile = $currentWorkingDirectory . DIRECTORY_SEPARATOR . $discoverableConfigName;
if (file_exists($discoverableConfigFile) && is_readable(($discoverableConfigFile))) {
$configFile = $discoverableConfigFile;
break;
}
}
}

$tmpDir = sys_get_temp_dir() . '/phpstan';
if (!empty($configFile)) {
$neonConfig = Neon::decode(file_get_contents($configFile));
if(is_array($neonConfig) && isset($neonConfig['parameters']) && isset($neonConfig['parameters']['tmpDir'])) {
$tmpDir = $neonConfig['parameters']['tmpDir'];
}
}

$cache = new Cache(new FileCacheStorage($tmpDir . '/cache/PHPStan'));

$mockAutoloader = new MockAutoloader();
$factoryAutoloader = new FactoryAutoloader($cache);
$proxyAutoloader = new ProxyAutoloader($cache);

\spl_autoload_register([$mockAutoloader, 'autoload'], true, true);
\spl_autoload_register([$factoryAutoloader, 'autoload'], true, false);
\spl_autoload_register([$proxyAutoloader, 'autoload'], true, false);
})($GLOBALS['argv'] ?? []);
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
],
"require": {
"php": "^7.2.0",
"nette/neon": "^3.1",
"phpstan/phpstan": "^0.12.18"
},
"require-dev": {
"bitexpert/phing-securitychecker": "^0.4.0",
"captainhook/captainhook": "^5.1.2",
"captainhook/plugin-composer": "^5.1.3",
"mikey179/vfsstream": "^1.6",
"nikic/php-parser": "^4.3",
"phpunit/phpunit": "^8.5.3",
"squizlabs/php_codesniffer": "^3.4",
"phing/phing": "^2.16",
"bitexpert/phing-securitychecker": "^0.4.0"
"phpunit/phpunit": "^8.5.3",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"bitExpert\\PHPStan\\": "src/bitExpert/PHPStan"
}
Expand Down
110 changes: 109 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 0 additions & 45 deletions registration.php

This file was deleted.

68 changes: 0 additions & 68 deletions src/bitExpert/PHPStan/Magento/Autoload/Autoloader.php

This file was deleted.

Loading

0 comments on commit f70a5a7

Please sign in to comment.