Skip to content

Commit 91f94c9

Browse files
authored
Merge pull request #70 from inpsyde/feature/65
Get rid of Neutron PHP Standard
2 parents f483017 + eb1a128 commit 91f94c9

16 files changed

+387
-167
lines changed

.github/workflows/quality-assurance-php.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- '**phpcs.xml.dist'
99
- '**phpunit.xml.dist'
1010
- '**psalm.xml'
11+
- '**ruleset.xml'
1112
workflow_dispatch:
1213
inputs:
1314
jobs:

.psalm/autoloader.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

Inpsyde/PhpcsHelpers.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ public static function isVoidReturn(
590590
*/
591591
public static function isNullReturn(File $file, int $returnPosition): bool
592592
{
593-
return
594-
!self::isVoidReturn($file, $returnPosition, false)
593+
return !self::isVoidReturn($file, $returnPosition, false)
595594
&& self::isVoidReturn($file, $returnPosition, true);
596595
}
597596

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the "php-coding-standards" package.
5+
*
6+
* Copyright (C) 2023 Inpsyde GmbH
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* as published by the Free Software Foundation; either version 2
11+
* of the License, or (at your option) any later version.
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace Inpsyde\Sniffs\CodeQuality;
24+
25+
use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;
26+
27+
class DisableCallUserFuncSniff extends AbstractFunctionRestrictionsSniff
28+
{
29+
/**
30+
* @return array<string, array<string, string|array>>
31+
*
32+
* phpcs:disable Inpsyde.CodeQuality.NoAccessors
33+
*/
34+
public function getGroups(): array
35+
{
36+
// phpcs:enable Inpsyde.CodeQuality.NoAccessors
37+
return [
38+
'call_user_func' => [
39+
'type' => 'error',
40+
'message' => 'Usage of %s() is forbidden.',
41+
'functions' => [
42+
'call_user_func',
43+
'call_user_func_array',
44+
],
45+
],
46+
];
47+
}
48+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the "php-coding-standards" package.
5+
*
6+
* Copyright (C) 2023 Inpsyde GmbH
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* as published by the Free Software Foundation; either version 2
11+
* of the License, or (at your option) any later version.
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace Inpsyde\Sniffs\CodeQuality;
24+
25+
use PHP_CodeSniffer\Files\File;
26+
use PHP_CodeSniffer\Sniffs\Sniff;
27+
use PHPCSUtils\Utils\FunctionDeclarations;
28+
use PHPCSUtils\Utils\Scopes;
29+
30+
class DisableMagicSerializeSniff implements Sniff
31+
{
32+
/** @var list<string> */
33+
public array $disabledFunctions = [
34+
'__serialize',
35+
'__sleep',
36+
'__unserialize',
37+
'__wakeup',
38+
];
39+
40+
/**
41+
* @return list<int>
42+
*/
43+
public function register(): array
44+
{
45+
return [T_FUNCTION];
46+
}
47+
48+
/**
49+
* @param File $phpcsFile
50+
* @param int $stackPtr
51+
* @return void
52+
*
53+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
54+
*/
55+
public function process(File $phpcsFile, $stackPtr)
56+
{
57+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
58+
if (!Scopes::isOOMethod($phpcsFile, $stackPtr)) {
59+
return;
60+
}
61+
62+
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
63+
if (in_array($name, $this->disabledFunctions, true)) {
64+
$phpcsFile->addError(
65+
sprintf(
66+
'The method "%s" is forbidden, please use Serializable interface.',
67+
$name
68+
),
69+
$stackPtr,
70+
'Found'
71+
);
72+
}
73+
}
74+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the "php-coding-standards" package.
5+
*
6+
* Copyright (C) 2023 Inpsyde GmbH
7+
*
8+
* This program is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License
10+
* as published by the Free Software Foundation; either version 2
11+
* of the License, or (at your option) any later version.
12+
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21+
*/
22+
23+
declare(strict_types=1);
24+
25+
namespace Inpsyde\Sniffs\CodeQuality;
26+
27+
use PHP_CodeSniffer\Files\File;
28+
use PHP_CodeSniffer\Sniffs\Sniff;
29+
use PHPCSUtils\Utils\FunctionDeclarations;
30+
use PHPCSUtils\Utils\Namespaces;
31+
use PHPCSUtils\Utils\ObjectDeclarations;
32+
use PHPCSUtils\Utils\Scopes;
33+
34+
class NoRootNamespaceFunctionsSniff implements Sniff
35+
{
36+
/**
37+
* @return list<int>
38+
*/
39+
public function register(): array
40+
{
41+
return [T_FUNCTION];
42+
}
43+
44+
/**
45+
* @param File $phpcsFile
46+
* @param int $stackPtr
47+
* @return void
48+
*
49+
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
50+
*/
51+
public function process(File $phpcsFile, $stackPtr): void
52+
{
53+
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
54+
if (Scopes::isOOMethod($phpcsFile, $stackPtr)) {
55+
return;
56+
}
57+
58+
$namespace = Namespaces::determineNamespace($phpcsFile, $stackPtr);
59+
if ($namespace !== '') {
60+
return;
61+
}
62+
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
63+
if (!$name) {
64+
return;
65+
}
66+
67+
$message = sprintf('The function "%s" is in root namespace.', $name);
68+
69+
$phpcsFile->addError($message, $stackPtr, 'Found');
70+
}
71+
}

Inpsyde/ruleset.xml

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<ruleset name="Inpsyde Coding Standard">
2+
<ruleset name="Inpsyde PHP Coding Standard">
33

44
<description>PHP 7+ coding standards for Inpsyde WordPress projects.</description>
55

@@ -13,45 +13,22 @@
1313
<exclude name="Generic.Files.LineLength.TooLong"/>
1414
</rule>
1515

16-
<!--
17-
Neutron standard are quality tools for PHP7 development from Automattic.
18-
See https://github.com/Automattic/phpcs-neutron-standard
19-
-->
20-
<rule ref="NeutronStandard.AssignAlign.DisallowAssignAlign">
21-
<type>warning</type>
22-
</rule>
23-
<rule ref="NeutronStandard.Functions.DisallowCallUserFunc">
24-
<type>warning</type>
25-
</rule>
26-
<rule ref="NeutronStandard.Globals.DisallowGlobalFunctions">
27-
<type>warning</type>
28-
</rule>
29-
<rule ref="NeutronStandard.MagicMethods.DisallowMagicSerialize">
30-
<type>warning</type>
31-
</rule>
32-
<rule ref="NeutronStandard.StrictTypes.RequireStrictTypes">
33-
<type>warning</type>
34-
</rule>
35-
<rule ref="NeutronStandard.Whitespace.DisallowMultipleNewlines">
36-
<type>warning</type>
37-
</rule>
38-
<rule ref="NeutronStandard.Whitespace.RequireNewlineBetweenFunctions">
39-
<type>warning</type>
40-
</rule>
41-
4216
<!--
4317
Curated list of WordPress specific rules.
4418
See https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
4519
-->
46-
<rule ref="WordPress.Arrays.CommaAfterArrayItem"/>
47-
<rule ref="WordPress.CodeAnalysis.AssignmentInCondition"/>
48-
<rule ref="WordPress.CodeAnalysis.EmptyStatement"/>
20+
<rule ref="WordPress.CodeAnalysis.AssignmentInTernaryCondition"/>
4921
<rule ref="WordPress.CodeAnalysis.EscapedNotTranslated"/>
5022
<rule ref="WordPress.DB.PreparedSQLPlaceholders"/>
5123
<rule ref="WordPress.DB.PreparedSQL"/>
5224
<rule ref="WordPress.DB.RestrictedClasses"/>
5325
<rule ref="WordPress.DB.RestrictedFunctions"/>
5426
<rule ref="WordPress.DateTime.CurrentTimeTimestamp"/>
27+
<rule ref="WordPress.DateTime.RestrictedFunctions">
28+
<properties>
29+
<property name="exclude" type="array" value="date"/>
30+
</properties>
31+
</rule>
5532
<rule ref="WordPress.NamingConventions.PrefixAllGlobals"/>
5633
<rule ref="WordPress.NamingConventions.ValidHookName">
5734
<properties>
@@ -81,7 +58,6 @@
8158
<rule ref="WordPress.PHP.POSIXFunctions"/>
8259
<rule ref="WordPress.PHP.PregQuoteDelimiter"/>
8360
<rule ref="WordPress.PHP.RestrictedPHPFunctions"/>
84-
<rule ref="WordPress.PHP.StrictComparisons"/>
8561
<rule ref="WordPress.PHP.StrictInArray"/>
8662
<rule ref="WordPress.PHP.TypeCasts"/>
8763
<rule ref="WordPress.Security.EscapeOutput"/>
@@ -131,10 +107,38 @@
131107
<exclude name="VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable" />
132108
</rule>
133109

110+
<!--
111+
PHPCS Extra
112+
-->
113+
<rule ref="NormalizedArrays.Arrays.CommaAfterLast" />
114+
<rule ref="Universal.Operators.StrictComparisons" />
115+
<rule ref="Universal.WhiteSpace.PrecisionAlignment" />
116+
117+
<!--
118+
Slevomat
119+
-->
120+
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration">
121+
<properties>
122+
<property name="allowMultiLine" type="boolean" value="yes"/>
123+
</properties>
124+
</rule>
125+
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
126+
<properties>
127+
<property name="spacesCountAroundEqualsSign" type="integer" value="0"/>
128+
</properties>
129+
</rule>
130+
<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces">
131+
<properties>
132+
<property type="boolean" name="ignoreSpacesInComment" value="yes" />
133+
</properties>
134+
</rule>
135+
134136
<!--
135137
Generic
136138
-->
137139
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
140+
<rule ref="Generic.CodeAnalysis.AssignmentInCondition"/>
141+
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
138142
<rule ref="Generic.Metrics.CyclomaticComplexity">
139143
<properties>
140144
<property name="absoluteComplexity" value="50"/>
@@ -144,6 +148,13 @@
144148
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
145149
<rule ref="Generic.PHP.LowerCaseConstant"/>
146150
<rule ref="Generic.VersionControl.GitMergeConflict"/>
151+
<rule ref="Generic.WhiteSpace.ArbitraryParenthesesSpacing">
152+
<properties>
153+
<property type="boolean" name="ignoreNewlines" value="yes" />
154+
</properties>
155+
</rule>
156+
<rule ref="Generic.WhiteSpace.LanguageConstructSpacing"/>
157+
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>
147158
<rule ref="Squiz.Classes.LowercaseClassKeywords"/>
148159
<rule ref="Squiz.PHP.CommentedOutCode">
149160
<properties>
@@ -156,6 +167,13 @@
156167
<rule ref="Squiz.PHP.NonExecutableCode"/>
157168
<rule ref="Squiz.Scope.MemberVarScope"/>
158169
<rule ref="Squiz.Scope.StaticThisUsage"/>
170+
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
171+
<properties>
172+
<property name="spacing" value="1"/>
173+
<property name="spacingBeforeFirst" value="0"/>
174+
<property name="spacingAfterLast" value="0"/>
175+
</properties>
176+
</rule>
159177

160178
<!--
161179
PHPCompatibility

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
"minimum-stability": "stable",
3131
"require": {
3232
"php": ">=7.4",
33-
"dealerdirect/phpcodesniffer-composer-installer": "~1.0.0",
3433
"automattic/vipwpcs": "dev-develop",
34+
"dealerdirect/phpcodesniffer-composer-installer": "~1.0.0",
3535
"phpcompatibility/php-compatibility": "^9.3.5",
36-
"automattic/phpcs-neutron-standard": "^v1.7.0"
36+
"phpcsstandards/phpcsextra": "^1.1",
37+
"phpcsstandards/phpcsutils": "^1.0",
38+
"slevomat/coding-standard": "^8.13"
3739
},
3840
"require-dev": {
3941
"phpunit/phpunit": "^9.6.11",
@@ -65,6 +67,7 @@
6567
]
6668
},
6769
"config": {
70+
"sort-packages": true,
6871
"allow-plugins": {
6972
"inpsyde/*": true,
7073
"composer/*": true,

0 commit comments

Comments
 (0)