Skip to content

Commit 2e592f6

Browse files
committed
added strict types declaration to all files, fixed issue with helper class cannot be instantiated, fixed typo, fixed issue with controllers in subdirectories cannot be loaded correctly, added missing dependencies, made /htdocs the default folder (instead of public), added core and community folders also to autoloader, removed default path in extension.neon (because this cannot be overwritten)
1 parent 3a571bb commit 2e592f6

File tree

10 files changed

+31
-15
lines changed

10 files changed

+31
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/composer.lock
22
/vendor/
3+
.idea

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Extension for [PHPStan](https://github.com/phpstan/phpstan) to allow analysis of
44

55
Currently it assumes Magento is installed in the public/ 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.
66

7-
By default phpstan with this extension will test public/app/code/local only.
8-
97
## Usage
108

119
Add `phpstan.neon` to your Magento 1 project.
@@ -33,6 +31,8 @@ composer require inviqa/phpstan-magento1 phpstan/phpstan-shim
3331

3432
## Alternative Magento path
3533

34+
By default this extension assumes the Magento directory is `%currentWorkingDirectory%/htdocs`.
35+
3636
Add to the project's phpstan.neon:
3737

3838
```neon

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
2-
"name": "inviqa/phpstan-magento1",
2+
"name": "vianetz/phpstan-magento1",
33
"description": "Extension for PHPStan to allow analysis of Magento 1 code.",
44
"type": "library",
55
"require": {
6-
"phpstan/phpstan": "0.11.*"
6+
"phpstan/phpstan": "0.11.*",
7+
"php": ">= 7.0",
8+
"nikic/php-parser": "^4.4"
9+
},
10+
"replace": {
11+
"inviqa/phpstan-magento1": "0.1.5"
712
},
813
"autoload": {
914
"psr-4": {

extension.neon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
parameters:
22
vendor_path: %currentWorkingDirectory%/vendor
3-
paths:
4-
- %currentWorkingDirectory%/public/app/code/local
5-
bootstrap: %vendor_path%/inviqa/phpstan-magento1/phpstan-bootstrap.php
3+
bootstrap: %vendor_path%/vianetz/phpstan-magento1/phpstan-bootstrap.php
64
excludes_analyse:
7-
- %currentWorkingDirectory%/public/app/code/local/*/*/data/*
8-
- %currentWorkingDirectory%/public/app/code/local/*/*/sql/*
5+
- %currentWorkingDirectory%/htdocs/app/code/local/*/*/data/*
6+
- %currentWorkingDirectory%/htdocs/app/code/local/*/*/sql/*
7+
autoload_files:
8+
- %currentWorkingDirectory%/htdocs/app/Mage.php
99

1010
services:
1111
-

phpstan-bootstrap.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
use PHPStanMagento1\Autoload\Magento\ModuleControllerAutoloader;
44

5-
if (!class_exists(Mage::class)) {
6-
require_once __DIR__ . '/../../../public/app/Mage.php';
7-
}
8-
95
(new ModuleControllerAutoloader('local'))->register();
6+
(new ModuleControllerAutoloader('core'))->register();
7+
(new ModuleControllerAutoloader('community'))->register();
108

119
// workaround Magento's use of date phpdoc typehint for string type
1210
// better would be to implement the typehint to make it appear string type

src/Autoload/Magento/ModuleControllerAutoloader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPStanMagento1\Autoload\Magento;
45

@@ -17,7 +18,7 @@ class ModuleControllerAutoloader
1718

1819
public function __construct($codePool, $magentoRoot = null)
1920
{
20-
if (empty($magentRoot)) {
21+
if (empty($magentoRoot)) {
2122
$mageClass = new ReflectionClass(Mage::class);
2223
if ($mageClass->getFileName() !== false) {
2324
$magentoRoot = dirname($mageClass->getFileName(), 2);
@@ -37,7 +38,8 @@ public function register()
3738
public function autoload($className)
3839
{
3940
if (preg_match('/^([a-zA-Z0-9\x7f-\xff]*)_([a-zA-Z0-9\x7f-\xff]*)_([a-zA-Z0-9_\x7f-\xff]+)/', $className, $match) === 1) {
40-
$controllerFilename = sprintf('%s/app/code/%s/%s/%s/controllers/%s.php', $this->magentoRoot, $this->codePool, $match[1], $match[2], $match[3]);
41+
$class = str_replace('_', '/', $match[3]);
42+
$controllerFilename = sprintf('%s/app/code/%s/%s/%s/controllers/%s.php', $this->magentoRoot, $this->codePool, $match[1], $match[2], $class);
4143
if (file_exists($controllerFilename)) {
4244
(function ($file) {
4345
include $file;

src/Reflection/Varien/Object/MagicMethodReflection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPStanMagento1\Reflection\Varien\Object;
45

src/Reflection/Varien/Object/MagicMethodsReflectionExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPStanMagento1\Reflection\Varien\Object;
45

src/Type/Mage/Core/Model/Layout/HelperMethodsReturnTypeExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPStanMagento1\Type\Mage\Core\Model\Layout;
45

src/Type/Mage/HelperMethodsReturnTypeExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPStanMagento1\Type\Mage;
45

@@ -8,6 +9,7 @@
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
12+
use PHPStan\Type\NullType;
1113
use PHPStan\Type\ObjectType;
1214
use PHPStan\Type\Type;
1315

@@ -42,6 +44,11 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
4244

4345
$name = $methodCall->args[0]->value->value;
4446
$class = $this->getClassFromHelperMethod($methodReflection->getName(), $name);
47+
48+
if ($class === false) {
49+
return new NullType();
50+
}
51+
4552
return new ObjectType($class);
4653
}
4754

0 commit comments

Comments
 (0)