-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement MissingMagicSerializationMethodsRule
- Loading branch information
1 parent
c5a696c
commit 74912c8
Showing
8 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ parameters: | |
notAnalysedTrait: true | ||
curlSetOptTypes: true | ||
listType: true | ||
missingMagicSerializationRule: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Rules/Methods/MissingMagicSerializationMethodsRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use Serializable; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class MissingMagicSerializationMethodsRule implements Rule | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpversion) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
if (!$this->phpversion->serializableRequiresMagicMethods()) { | ||
return []; | ||
} | ||
if (!$classReflection->implementsInterface(Serializable::class)) { | ||
return []; | ||
} | ||
if ($classReflection->isAbstract() || $classReflection->isInterface() || $classReflection->isEnum()) { | ||
return []; | ||
} | ||
|
||
$messages = []; | ||
|
||
try { | ||
$nativeMethods = $classReflection->getNativeReflection()->getMethods(); | ||
} catch (IdentifierNotFound) { | ||
return []; | ||
} | ||
|
||
$missingMagicSerialize = true; | ||
$missingMagicUnserialize = true; | ||
foreach ($nativeMethods as $method) { | ||
if (strtolower($method->getName()) === '__serialize') { | ||
$missingMagicSerialize = false; | ||
} | ||
if (strtolower($method->getName()) !== '__unserialize') { | ||
continue; | ||
} | ||
|
||
$missingMagicUnserialize = false; | ||
} | ||
|
||
if ($missingMagicSerialize) { | ||
$messages[] = RuleErrorBuilder::message(sprintf( | ||
'Non-abstract class %s implements the Serializable interface, but does not implement __serialize().', | ||
$classReflection->getDisplayName(), | ||
))->tip('See https://wiki.php.net/rfc/phase_out_serializable')->build(); | ||
} | ||
if ($missingMagicUnserialize) { | ||
$messages[] = RuleErrorBuilder::message(sprintf( | ||
'Non-abstract class %s implements the Serializable interface, but does not implement __unserialize().', | ||
$classReflection->getDisplayName(), | ||
))->tip('See https://wiki.php.net/rfc/phase_out_serializable')->build(); | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
tests/PHPStan/Rules/Methods/MissingMagicSerializationMethodsRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<MissingMagicSerializationMethodsRule> | ||
*/ | ||
class MissingMagicSerializationMethodsRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new MissingMagicSerializationMethodsRule(new PhpVersion(PHP_VERSION_ID)); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
$this->markTestSkipped('Test requires PHP 8.1.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/missing-serialization.php'], [ | ||
[ | ||
'Non-abstract class MissingMagicSerializationMethods\myObj implements the Serializable interface, but does not implement __serialize().', | ||
14, | ||
'See https://wiki.php.net/rfc/phase_out_serializable', | ||
], | ||
[ | ||
'Non-abstract class MissingMagicSerializationMethods\myObj implements the Serializable interface, but does not implement __unserialize().', | ||
14, | ||
'See https://wiki.php.net/rfc/phase_out_serializable', | ||
], | ||
]); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
tests/PHPStan/Rules/Methods/data/missing-serialization.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php // lint >= 8.1 | ||
|
||
namespace MissingMagicSerializationMethods; | ||
|
||
use Serializable; | ||
|
||
abstract class abstractObj implements Serializable { | ||
public function serialize() { | ||
} | ||
public function unserialize($data) { | ||
} | ||
} | ||
|
||
class myObj implements Serializable { | ||
public function serialize() { | ||
} | ||
public function unserialize($data) { | ||
} | ||
} | ||
|
||
enum myEnum implements Serializable { | ||
case X; | ||
case Y; | ||
|
||
public function serialize() { | ||
} | ||
public function unserialize($data) { | ||
} | ||
} | ||
|
||
abstract class allGood implements Serializable { | ||
public function serialize() { | ||
} | ||
public function unserialize($data) { | ||
} | ||
public function __serialize() { | ||
} | ||
public function __unserialize($data) { | ||
} | ||
} |