-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
- 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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Inpsyde\Sniffs\CodeQuality; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHPCSUtils\Utils\ObjectDeclarations; | ||
|
||
class DisableSerializeInterfaceSniff implements Sniff | ||
{ | ||
/** | ||
* @return list<int> | ||
Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.1) / static-analysis-phpInvalidReturnType
Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.3) / static-analysis-phpInvalidReturnType
Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.0) / static-analysis-phpInvalidReturnType
Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (7.4) / static-analysis-phpInvalidReturnType
Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.2) / static-analysis-phpInvalidReturnType
|
||
*/ | ||
public function register(): array | ||
{ | ||
return [ | ||
Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.1) / static-analysis-phpInvalidReturnStatement
Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.3) / static-analysis-phpInvalidReturnStatement
Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.0) / static-analysis-phpInvalidReturnStatement
Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (7.4) / static-analysis-phpInvalidReturnStatement
Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.2) / static-analysis-phpInvalidReturnStatement
|
||
\T_CLASS, | ||
\T_ANON_CLASS, | ||
\T_ENUM, | ||
\T_INTERFACE, | ||
]; | ||
} | ||
|
||
/** | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return void | ||
* | ||
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr): void | ||
{ | ||
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration | ||
$tokenCode = $phpcsFile->getTokens()[$stackPtr]['code']; | ||
Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.1) / static-analysis-phpMixedArrayAccess
Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.3) / static-analysis-phpMixedArrayAccess
Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.0) / static-analysis-phpMixedArrayAccess
Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (7.4) / static-analysis-phpMixedArrayAccess
Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / static-code-analysis-php (8.2) / static-analysis-phpMixedArrayAccess
|
||
$find = ($tokenCode === \T_INTERFACE) | ||
? ObjectDeclarations::findExtendedInterfaceNames($phpcsFile, $stackPtr) | ||
: ObjectDeclarations::findImplementedInterfaceNames($phpcsFile, $stackPtr); | ||
|
||
if (($find === false) || !in_array('Serializable', $find, true)) { | ||
return; | ||
} | ||
|
||
$phpcsFile->addError( | ||
'The Serializable interface is deprecated, please use __serialize and __unserialize instead.', | ||
Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / coding-standards-analysis-php / coding-standards-php
Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php GitHub Actions / coding-standards-analysis-php / coding-standards-php
|
||
$stackPtr, | ||
'Found' | ||
); | ||
} | ||
} |