Skip to content

Commit 7481c1a

Browse files
authored
Merge pull request ateli-development#2 from vianetz/compatibility_with_phpstan_v0.12
Compatibility with phpstan v0.12
2 parents 259d1fd + acf1196 commit 7481c1a

19 files changed

+279
-169
lines changed

README.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# phpstan-magento1 extension
1+
# PHPStan Extension for Magento 1
22

33
Extension for [PHPStan](https://github.com/phpstan/phpstan) to allow analysis of Magento 1 code.
44

5-
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.
5+
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.
66

77
## Usage
88

@@ -12,23 +12,15 @@ Make sure it has
1212

1313
```neon
1414
includes:
15-
- vendor/inviqa/phpstan-magento1/extension.neon
15+
- vendor/vianetz/phpstan-magento1/extension.neon
1616
```
1717

18-
Whilst this extension depends on phpstan/phpstan, it can also depend on phpstan/phpstan-shim, which decouples its dependencies from the project's own use of them.
19-
20-
With coupled dependencies:
18+
Then run
2119

2220
```bash
2321
composer require inviqa/phpstan-magento1 phpstan/phpstan
2422
```
2523

26-
With uncoupled phar package:
27-
28-
```bash
29-
composer require inviqa/phpstan-magento1 phpstan/phpstan-shim
30-
```
31-
3224
## Alternative Magento path
3325

3426
By default this extension assumes the Magento directory is `%currentWorkingDirectory%/htdocs`.
@@ -39,9 +31,6 @@ Add to the project's phpstan.neon:
3931
parameters:
4032
paths:
4133
- %currentWorkingDirectory%/path/to/magento/app/code/local
42-
excludes_analyse:
43-
- %currentWorkingDirectory%/path/to/magento/app/code/local/*/*/data/*
44-
- %currentWorkingDirectory%/path/to/magento/app/code/local/*/*/sql/*
4534
autoload_files:
4635
- %currentWorkingDirectory%/path/to/magento/app/Mage.php
4736
```

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"description": "Extension for PHPStan to allow analysis of Magento 1 code.",
44
"type": "library",
55
"require": {
6-
"phpstan/phpstan": "^0.11",
7-
"php": ">= 7.2",
8-
"nikic/php-parser": "^4.4"
6+
"phpstan/phpstan": "^0.11|^0.12",
7+
"php": ">= 7.2"
98
},
109
"replace": {
1110
"inviqa/phpstan-magento1": "0.1.5"

extension.neon

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
parameters:
2-
vendor_path: %currentWorkingDirectory%/vendor
3-
bootstrap: %vendor_path%/vianetz/phpstan-magento1/phpstan-bootstrap.php
2+
bootstrap: %currentWorkingDirectory%/vendor/vianetz/phpstan-magento1/phpstan-bootstrap.php
43
excludes_analyse:
5-
- %currentWorkingDirectory%/htdocs/app/code/local/*/*/data/*
6-
- %currentWorkingDirectory%/htdocs/app/code/local/*/*/sql/*
4+
- */app/code/local/*/*/data/*
5+
- */app/code/local/*/*/sql/*
76
autoload_files:
87
- %currentWorkingDirectory%/htdocs/app/Mage.php
98

@@ -13,10 +12,26 @@ services:
1312
tags:
1413
- phpstan.broker.methodsClassReflectionExtension
1514
-
16-
class: PHPStanMagento1\Type\Mage\Core\Model\Layout\HelperMethodsReturnTypeExtension
15+
class: PHPStanMagento1\Type\Mage\CoreModelLayout\Helper
1716
tags:
1817
- phpstan.broker.dynamicMethodReturnTypeExtension
1918
-
20-
class: PHPStanMagento1\Type\Mage\HelperMethodsReturnTypeExtension
19+
class: PHPStanMagento1\Type\Mage\CoreModelLayout\GetBlockSingleton
20+
tags:
21+
- phpstan.broker.dynamicMethodReturnTypeExtension
22+
-
23+
class: PHPStanMagento1\Type\Mage\GetModel
24+
tags:
25+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
26+
-
27+
class: PHPStanMagento1\Type\Mage\GetResourceModel
2128
tags:
2229
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
30+
-
31+
class: PHPStanMagento1\Type\Mage\GetSingleton
32+
tags:
33+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
34+
-
35+
class: PHPStanMagento1\Type\Mage\Helper
36+
tags:
37+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension

phpstan-bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
// better would be to implement the typehint to make it appear string type
1212
class date {}
1313

14-
Mage::app();
14+
Mage::app();

src/Autoload/Magento/ModuleControllerAutoloader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function register(): void
3333
spl_autoload_register([$this, 'autoload']);
3434
}
3535

36-
public function autoload($className): void
36+
public function autoload(string $className): void
3737
{
3838
if (preg_match('/^([a-zA-Z0-9\x7f-\xff]*)_([a-zA-Z0-9\x7f-\xff]*)_([a-zA-Z0-9_\x7f-\xff]+)/', $className, $match) === 1) {
3939
$class = str_replace('_', '/', $match[3]);

src/Reflection/Varien/Object/MagicMethodReflection.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,39 @@ public function getVariants(): array
6161
new TrivialParametersAcceptor(),
6262
];
6363
}
64+
65+
public function getDocComment(): ?string
66+
{
67+
return null;
68+
}
69+
70+
public function isDeprecated(): \PHPStan\TrinaryLogic
71+
{
72+
return \PHPStan\TrinaryLogic::createMaybe();
73+
}
74+
75+
public function getDeprecatedDescription(): ?string
76+
{
77+
return null;
78+
}
79+
80+
public function isFinal(): \PHPStan\TrinaryLogic
81+
{
82+
return \PHPStan\TrinaryLogic::createNo();
83+
}
84+
85+
public function isInternal(): \PHPStan\TrinaryLogic
86+
{
87+
return \PHPStan\TrinaryLogic::createMaybe();
88+
}
89+
90+
public function getThrowType(): ?\PHPStan\Type\Type
91+
{
92+
return null;
93+
}
94+
95+
public function hasSideEffects(): \PHPStan\TrinaryLogic
96+
{
97+
return \PHPStan\TrinaryLogic::createMaybe();
98+
}
6499
}

src/Reflection/Varien/Object/MagicMethodsReflectionExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class MagicMethodsReflectionExtension implements MethodsClassReflectionExt
1414
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
1515
{
1616
$magentoMagicMethods = ['get', 'set', 'uns', 'has'];
17-
return $classReflection->isSubclassOf(Varien_Object::class)
17+
return ($classReflection->isSubclassOf(Varien_Object::class) || $classReflection->getName() === Varien_Object::class)
1818
&& \in_array(substr($methodName, 0, 3), $magentoMagicMethods, true);
1919
}
2020

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

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage\CoreModelLayout;
5+
6+
final class GetBlockSingleton extends MethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getBlockClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'getBlockSingleton';
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage\CoreModelLayout;
5+
6+
final class Helper extends MethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getHelperClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'helper';
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage\CoreModelLayout;
5+
6+
abstract class MethodReturnTypeDetector extends \PHPStanMagento1\Type\Mage\MethodReturnTypeDetector implements \PHPStan\Type\DynamicMethodReturnTypeExtension
7+
{
8+
public function getClass(): string
9+
{
10+
return \Mage_Core_Model_Layout::class;
11+
}
12+
}

src/Type/Mage/GetModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage;
5+
6+
final class GetModel extends StaticMethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getModelClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'getModel';
16+
}
17+
}

src/Type/Mage/GetResourceModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage;
5+
6+
final class GetResourceModel extends StaticMethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getResourceModelClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'getResourceModel';
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage;
5+
6+
final class GetResourceSingleton extends StaticMethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getResourceModelClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'getResourceSingleton';
16+
}
17+
}

src/Type/Mage/GetSingleton.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage;
5+
6+
final class GetSingleton extends StaticMethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getModelClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'getSingleton';
16+
}
17+
}

src/Type/Mage/Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHPStanMagento1\Type\Mage;
5+
6+
final class Helper extends StaticMethodReturnTypeDetector
7+
{
8+
public function getMagentoClassName(string $identifier): string
9+
{
10+
return $this->getMagentoConfig()->getHelperClassName($identifier);
11+
}
12+
13+
protected static function getMethodName(): string
14+
{
15+
return 'helper';
16+
}
17+
}

0 commit comments

Comments
 (0)