Skip to content

Commit

Permalink
Make phpstan work without db connection
Browse files Browse the repository at this point in the history
- do not include/run Mage class
- provide helper class for loading Magento and
Modules XML configuration
- introduce new configuration parameter "magentoRootPath"
  • Loading branch information
tmotyl committed Sep 27, 2021
1 parent 060a5b4 commit 9ce826b
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 28 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# PHPStan Extension for Magento 1
# PHPStan Extension for Magento 1 / OpenMage MageLTS

Extension for [PHPStan](https://github.com/phpstan/phpstan) to allow analysis of Magento 1 code.
It doesn't require db connection to run.

Currently it assumes Magento is installed in the `htdocs/` directory of the project root. Further work is needed in phpstan itself to allow more intellegence for extensions to be more customised whilst working with both phpstan/phpstan and phpstan/phpstan-shim.

## Usage

Expand All @@ -12,32 +12,47 @@ Make sure it has

```neon
includes:
- vendor/vianetz/phpstan-magento1/extension.neon
- vendor/macopedia/phpstan-magento1/extension.neon
```

Then run

```bash
composer require inviqa/phpstan-magento1 phpstan/phpstan
composer require --dev macopedia/phpstan-magento1
```

## Alternative Magento path

By default this extension assumes the Magento directory is `%currentWorkingDirectory%/htdocs`.

You can adapt the path by changing the `magentoRootPath` parameter in the phpstan.neon file.
Add to the project's phpstan.neon:

```neon
parameters:
magentoRootPath: %currentWorkingDirectory%/htdocs
```

## Example configuration file for analysing Magento Module

```neon
parameters:
magentoRootPath: %currentWorkingDirectory%/htdocs
paths:
- %currentWorkingDirectory%/path/to/magento/app/code/local
autoload_files:
- %currentWorkingDirectory%/path/to/magento/app/Mage.php
paths:
#lets start small with just our extensions
- %currentWorkingDirectory%/app/code/local/VendorName/ModuleName
excludePaths:
- */Vendor/ModuleName/SomePathToExclude/*
level: 0
```

# Known Issues

## Data/SQL scripts can't be tested
## Data/SQL scripts can't be tested

Since these scripts use a presumed $this variable due to being included from a setup class, work is needed to:

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "vianetz/phpstan-magento1",
"name": "macopedia/phpstan-magento1",
"description": "Extension for PHPStan to allow analysis of Magento 1 code.",
"type": "library",
"require": {
"phpstan/phpstan": "^0.11|^0.12",
"phpstan/phpstan": "^0.12",
"php": ">= 7.2"
},
"replace": {
"inviqa/phpstan-magento1": "0.1.5"
"inviqa/phpstan-magento1": "0.1.5",
"vianetz/phpstan-magento1": "0.1.5"
},
"autoload": {
"psr-4": {
Expand Down
17 changes: 14 additions & 3 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
parametersSchema:
magentoRootPath: string()
parameters:
excludes_analyse:
magentoRootPath: %currentWorkingDirectory%/htdocs
excludePaths:
- */app/code/local/*/*/data/*
- */app/code/local/*/*/sql/*
bootstrapFiles:
- %currentWorkingDirectory%/htdocs/app/Mage.php
- phpstan-bootstrap.php
scanFiles:
- %magentoRootPath%/app/Mage.php
- %magentoRootPath%/app/code/core/Mage/Core/functions.php
scanDirectories:
#for static reflection, so we don't have to call autoloader
- %magentoRootPath%/lib/
- %magentoRootPath%/app/code/local/
- %magentoRootPath%/app/code/community/
- %magentoRootPath%/app/code/core/
typeAliases:
Mage_Catalog_Model_Entity_Product_Collection: 'Mage_Catalog_Model_Resource_Product_Collection'
callback: 'callable'
Expand Down Expand Up @@ -37,4 +48,4 @@ services:
-
class: PHPStanMagento1\Type\Mage\Helper
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
39 changes: 35 additions & 4 deletions phpstan-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,48 @@

use PHPStanMagento1\Autoload\Magento\ModuleControllerAutoloader;

if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('PS')) {
define('PS', PATH_SEPARATOR);
}

/**
* @var $container \PHPStan\DependencyInjection\MemoizingContainer
*/
$magentoRootPath = $container->getParameter('magentoRootPath');
if (empty($magentoRootPath)) {
throw new \Exception('Please set "magentoRootPath" in your phpstan.neon.');
}

if (!defined('BP')) {
define('BP', $magentoRootPath);
}

/**
* Set include path
*/
$paths = [];
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';

$appPath = implode(PS, $paths);
set_include_path($appPath . PS . get_include_path());
include_once "Mage/Core/functions.php";

(new ModuleControllerAutoloader('local'))->register();
(new ModuleControllerAutoloader('core'))->register();
(new ModuleControllerAutoloader('community'))->register();

/**
* We replace the original Varien_Autoload autoloader with a custom one.
* Custom autoloader compatible with Varien_Autoload
* Autoloading is needed only for the PHPStanMagento1\Config\MagentoCore which inherits from some magento classes.
* PHPStan uses static analysis, so doesn't require autoloading.
*/
spl_autoload_register(static function($className) {
spl_autoload_unregister([Varien_Autoload::instance(), 'autoload']);

$classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $className)));
$classFile .= '.php';
Expand All @@ -22,5 +55,3 @@
}
}
}, true, true);

Mage::app();
7 changes: 1 addition & 6 deletions src/Autoload/Magento/ModuleControllerAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ final class ModuleControllerAutoloader
public function __construct(string $codePool, $magentoRoot = null)
{
if (empty($magentoRoot)) {
$mageClass = new ReflectionClass(Mage::class);
if ($mageClass->getFileName() !== false) {
$magentoRoot = \dirname($mageClass->getFileName(), 2);
} else {
throw new \RuntimeException('Could not find path to Mage class');
}
$magentoRoot = \dirname(BP, 2);
}
$this->codePool = $codePool;
$this->magentoRoot = $magentoRoot;
Expand Down
Loading

0 comments on commit 9ce826b

Please sign in to comment.