Skip to content

Commit 89128a5

Browse files
authored
Merge pull request #90 from inpsyde/fix/update-serialization-mechanism
Update serialization mechanism
2 parents 236ee34 + 7104de0 commit 89128a5

File tree

5 files changed

+141
-36
lines changed

5 files changed

+141
-36
lines changed

Diff for: Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111

1212
class DisableMagicSerializeSniff implements Sniff
1313
{
14-
/** @var list<string> */
14+
/** @var array<string, string> */
1515
public array $disabledFunctions = [
16-
'__serialize',
17-
'__sleep',
18-
'__unserialize',
19-
'__wakeup',
16+
'__sleep' => '__serialize',
17+
'__wakeup' => '__unserialize',
2018
];
2119

2220
/**
@@ -42,12 +40,10 @@ public function process(File $phpcsFile, $stackPtr): void
4240
}
4341

4442
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
45-
if (in_array($name, $this->disabledFunctions, true)) {
43+
$alternative = $this->disabledFunctions[$name] ?? null;
44+
if ($alternative !== null) {
4645
$phpcsFile->addError(
47-
sprintf(
48-
'The method "%s" is forbidden, please use Serializable interface.',
49-
$name
50-
),
46+
"The method '{$name}' is deprecated, please use '{$alternative}' instead.",
5147
$stackPtr,
5248
'Found'
5349
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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|string>
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'] ?? null;
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, '
47+
. 'please use __serialize and __unserialize instead.',
48+
$stackPtr,
49+
'Found'
50+
);
51+
}
52+
}

Diff for: README.md

+25-24
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,31 @@ Some rules are also included from PHP_CodeSniffer itself, as well as [PHPCSExtra
117117

118118
The following custom rules are in use:
119119

120-
| Sniff Name | Description | Has Config | Auto-Fixable |
121-
|:---------------------------|:-----------------------------------------------------------------------------------------------|:----------:|:------------:|
122-
| `ArgumentTypeDeclaration` | Enforce argument type declaration. | | |
123-
| `DisableCallUserFunc` | Disable usage of `call_user_func`. | | |
124-
| `DisableMagicSerialize` | Disable usage of `__serialize`, `__sleep`, `__unserialize`, `__wakeup`. | | |
125-
| `DisallowShortOpenTag` | Disallow short open PHP tag (short echo tag allowed). | | |
126-
| `ElementNameMinimalLength` | Use minimum 3 chars for names (with a few exclusions) || |
127-
| `EncodingComment` | Detect usage of opening `-*- coding: utf-8 -*-` |||
128-
| `ForbiddenPublicProperty` | No public class properties | | |
129-
| `FunctionBodyStart` | Handle blank line at start of function body. | ||
130-
| `FunctionLength` | Max 50 lines per function/method, excluding blank lines and comments-only lines. || |
131-
| `HookClosureReturn` | Ensure that actions callbacks do not return anything, while filter callbacks return something. | | |
132-
| `HookPriority` | Report usage of `PHP_INT_MAX` and `PHP_INT_MIN` as hook priority. | | |
133-
| `LineLength` | Max 100 chars per line || |
134-
| `NestingLevel` | Max indent level of 3 inside functions || |
135-
| `NoAccessors` | Discourage usage of getters and setters. | | |
136-
| `NoElse` | Discourage usage of `else`. | | |
137-
| `NoRootNamespaceFunctions` | Report usage of global functions in the root namespace. | | |
138-
| `NoTopLevelDefine` | Discourage usage of `define` where `const` is preferable. | | |
139-
| `PropertyPerClassLimit` | Discourage usage of more than 10 properties per class. || |
140-
| `Psr4` | Check PSR-4 compliance || |
141-
| `ReturnTypeDeclaration` | Enforce return type declaration | | |
142-
| `StaticClosure` | Points closures that can be `static`. | ||
143-
| `VariablesName` | Check variable (and properties) names || |
120+
| Sniff Name | Description | Has Config | Auto-Fixable |
121+
|:----------------------------|:-----------------------------------------------------------------------------------------------|:----------:|:------------:|
122+
| `ArgumentTypeDeclaration` | Enforce argument type declaration. | | |
123+
| `DisableCallUserFunc` | Disable usage of `call_user_func`. | | |
124+
| `DisableMagicSerialize` | Disable usage of `__sleep`, `__wakeup`. | | |
125+
| `DisableSerializeInterface` | Disable usage of `Serializable` interface. | | |
126+
| `DisallowShortOpenTag` | Disallow short open PHP tag (short echo tag allowed). | | |
127+
| `ElementNameMinimalLength` | Use minimum 3 chars for names (with a few exclusions) || |
128+
| `EncodingComment` | Detect usage of opening `-*- coding: utf-8 -*-` |||
129+
| `ForbiddenPublicProperty` | No public class properties | | |
130+
| `FunctionBodyStart` | Handle blank line at start of function body. | ||
131+
| `FunctionLength` | Max 50 lines per function/method, excluding blank lines and comments-only lines. || |
132+
| `HookClosureReturn` | Ensure that actions callbacks do not return anything, while filter callbacks return something. | | |
133+
| `HookPriority` | Report usage of `PHP_INT_MAX` and `PHP_INT_MIN` as hook priority. | | |
134+
| `LineLength` | Max 100 chars per line || |
135+
| `NestingLevel` | Max indent level of 3 inside functions || |
136+
| `NoAccessors` | Discourage usage of getters and setters. | | |
137+
| `NoElse` | Discourage usage of `else`. | | |
138+
| `NoRootNamespaceFunctions` | Report usage of global functions in the root namespace. | | |
139+
| `NoTopLevelDefine` | Discourage usage of `define` where `const` is preferable. | | |
140+
| `PropertyPerClassLimit` | Discourage usage of more than 10 properties per class. || |
141+
| `Psr4` | Check PSR-4 compliance || |
142+
| `ReturnTypeDeclaration` | Enforce return type declaration | | |
143+
| `StaticClosure` | Points closures that can be `static`. | ||
144+
| `VariablesName` | Check variable (and properties) names || |
144145

145146
For **notes and configuration**, refer to the [`inpsyde-custom-sniffs.md`](/inpsyde-custom-sniffs.md)
146147
file in this repository.

Diff for: tests/unit/fixtures/disallow-magic-serialize.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class Foo {
66

7-
// @phpcsErrorOnNextLine
87
public function __serialize(): array
98
{
109
return [];
@@ -37,7 +36,6 @@ public function wakeup(): array
3736
return [];
3837
}
3938

40-
// @phpcsErrorOnNextLine
4139
public function __unserialize(): array
4240
{
4341
return [];

Diff for: tests/unit/fixtures/disallow-serialize-interface.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
// @phpcsSniff Inpsyde.CodeQuality.DisableSerializeInterface
4+
5+
// @phpcsErrorOnNextLine
6+
class One implements Serializable {
7+
8+
public function serialize()
9+
{
10+
return null;
11+
}
12+
13+
public function unserialize($data)
14+
{
15+
}
16+
}
17+
18+
// @phpcsErrorOnNextLine
19+
$x = new class implements Serializable {
20+
21+
public function serialize()
22+
{
23+
return null;
24+
}
25+
26+
public function unserialize($data)
27+
{
28+
}
29+
};
30+
31+
class Three {
32+
33+
public function serialize()
34+
{
35+
return null;
36+
}
37+
38+
public function unserialize($data)
39+
{
40+
}
41+
}
42+
43+
// @phpcsErrorOnNextLine
44+
interface Two extends Serializable {
45+
46+
}
47+
48+
class Four {
49+
50+
public function __serialize()
51+
{
52+
return null;
53+
}
54+
55+
public function __unserialize($data)
56+
{
57+
}
58+
}

0 commit comments

Comments
 (0)