Skip to content

Commit

Permalink
Refactor autoload implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shochdoerfer committed Apr 18, 2020
1 parent 1b0e233 commit 55381e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 89 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
21 changes: 18 additions & 3 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
* 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 PHPStan\Cache\Cache;

// This autoloader implementation supersedes the former \bitExpert\PHPStan\Magento\Autoload\Autoload implementation
(function () {
$cache = new Cache(new FileCacheStorage(sys_get_temp_dir() . '/phpstan/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);
})();
68 changes: 0 additions & 68 deletions src/bitExpert/PHPStan/Magento/Autoload/Autoloader.php

This file was deleted.

0 comments on commit 55381e2

Please sign in to comment.