Skip to content

Commit 24bb1fc

Browse files
committed
Update serialization mechanism
- Suggest suggest __serialize and __unserialize (previously discouraged) - Add sniff to discourage Serializable interface See - https://wiki.php.net/rfc/custom_object_serialization - https://www.php.net/manual/en/class.serializable.php
1 parent 236ee34 commit 24bb1fc

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ class DisableMagicSerializeSniff implements Sniff
1313
{
1414
/** @var list<string> */
1515
public array $disabledFunctions = [
16-
'__serialize',
1716
'__sleep',
18-
'__unserialize',
1917
'__wakeup',
2018
];
2119

@@ -45,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr): void
4543
if (in_array($name, $this->disabledFunctions, true)) {
4644
$phpcsFile->addError(
4745
sprintf(
48-
'The method "%s" is forbidden, please use Serializable interface.',
46+
'The method "%s" is deprecated, please use __serialize and __unserialize instead.',
4947
$name
5048
),
5149
$stackPtr,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Inpsyde\Sniffs\CodeQuality;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHPCSUtils\Utils\ObjectDeclarations;
10+
11+
class DisableSerializeInterfaceSniff implements Sniff
12+
{
13+
/**
14+
* @return list<int>
15+
*/
16+
public function register(): array
17+
{
18+
return [
19+
\T_CLASS,
20+
\T_ANON_CLASS,
21+
\T_ENUM,
22+
\T_INTERFACE,
23+
];
24+
}
25+
26+
/**
27+
* @param File $phpcsFile
28+
* @param int $stackPtr
29+
* @return void
30+
*
31+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
32+
*/
33+
public function process(File $phpcsFile, $stackPtr): void
34+
{
35+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
36+
$tokenCode = $phpcsFile->getTokens()[$stackPtr]['code'];
37+
$find = ($tokenCode === \T_INTERFACE)
38+
? ObjectDeclarations::findExtendedInterfaceNames($phpcsFile, $stackPtr)
39+
: ObjectDeclarations::findImplementedInterfaceNames($phpcsFile, $stackPtr);
40+
41+
if (($find === false) || !in_array('Serializable', $find, true)) {
42+
return;
43+
}
44+
45+
$phpcsFile->addError(
46+
'The Serializable interface is deprecated, please use __serialize and __unserialize instead.',
47+
$stackPtr,
48+
'Found'
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)