Skip to content

Commit

Permalink
Update serialization mechanism
Browse files Browse the repository at this point in the history
- 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
gmazzap committed Apr 16, 2024
1 parent 236ee34 commit 24bb1fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class DisableMagicSerializeSniff implements Sniff
{
/** @var list<string> */
public array $disabledFunctions = [
'__serialize',
'__sleep',
'__unserialize',
'__wakeup',
];

Expand Down Expand Up @@ -45,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr): void
if (in_array($name, $this->disabledFunctions, true)) {
$phpcsFile->addError(
sprintf(
'The method "%s" is forbidden, please use Serializable interface.',
'The method "%s" is deprecated, please use __serialize and __unserialize instead.',

Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 104 characters.

Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 104 characters.

Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 104 characters.
$name
),
$stackPtr,
Expand Down
51 changes: 51 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php
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

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.1) / static-analysis-php

InvalidReturnType

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:14:16: InvalidReturnType: The declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register is incorrect, got 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' (see https://psalm.dev/011)

Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.3) / static-analysis-php

InvalidReturnType

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:14:16: InvalidReturnType: The declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register is incorrect, got 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' (see https://psalm.dev/011)

Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.0) / static-analysis-php

InvalidReturnType

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:14:16: InvalidReturnType: The declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register is incorrect, got 'list{332, 'PHPCS_T_ANON_CLASS', 'PHPCS_T_ENUM', 334}' (see https://psalm.dev/011)

Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (7.4) / static-analysis-php

InvalidReturnType

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:14:16: InvalidReturnType: The declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register is incorrect, got 'list{364, 'PHPCS_T_ANON_CLASS', 'PHPCS_T_ENUM', 366}' (see https://psalm.dev/011)

Check failure on line 14 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.2) / static-analysis-php

InvalidReturnType

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:14:16: InvalidReturnType: The declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register is incorrect, got 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' (see https://psalm.dev/011)
*/
public function register(): array
{
return [

Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.1) / static-analysis-php

InvalidReturnStatement

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:18:16: InvalidReturnStatement: The inferred type 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' does not match the declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register (see https://psalm.dev/128)

Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.3) / static-analysis-php

InvalidReturnStatement

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:18:16: InvalidReturnStatement: The inferred type 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' does not match the declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register (see https://psalm.dev/128)

Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.0) / static-analysis-php

InvalidReturnStatement

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:18:16: InvalidReturnStatement: The inferred type 'list{332, 'PHPCS_T_ANON_CLASS', 'PHPCS_T_ENUM', 334}' does not match the declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register (see https://psalm.dev/128)

Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (7.4) / static-analysis-php

InvalidReturnStatement

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:18:16: InvalidReturnStatement: The inferred type 'list{364, 'PHPCS_T_ANON_CLASS', 'PHPCS_T_ENUM', 366}' does not match the declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register (see https://psalm.dev/128)

Check failure on line 18 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.2) / static-analysis-php

InvalidReturnStatement

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:18:16: InvalidReturnStatement: The inferred type 'list{333, 'PHPCS_T_ANON_CLASS', 336, 335}' does not match the declared return type 'list<int>' for Inpsyde\Sniffs\CodeQuality\DisableSerializeInterfaceSniff::register (see https://psalm.dev/128)
\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

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.1) / static-analysis-php

MixedArrayAccess

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:36:22: MixedArrayAccess: Cannot access array value on mixed variable (see https://psalm.dev/051)

Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.3) / static-analysis-php

MixedArrayAccess

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:36:22: MixedArrayAccess: Cannot access array value on mixed variable (see https://psalm.dev/051)

Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.0) / static-analysis-php

MixedArrayAccess

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:36:22: MixedArrayAccess: Cannot access array value on mixed variable (see https://psalm.dev/051)

Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (7.4) / static-analysis-php

MixedArrayAccess

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:36:22: MixedArrayAccess: Cannot access array value on mixed variable (see https://psalm.dev/051)

Check failure on line 36 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-php (8.2) / static-analysis-php

MixedArrayAccess

Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php:36:22: MixedArrayAccess: Cannot access array value on mixed variable (see https://psalm.dev/051)
$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

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 107 characters.

Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 107 characters.

Check warning on line 46 in Inpsyde/Sniffs/CodeQuality/DisableSerializeInterfaceSniff.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Line 46 exceeds 100 characters; contains 107 characters.
$stackPtr,
'Found'
);
}
}

0 comments on commit 24bb1fc

Please sign in to comment.