Skip to content

Commit

Permalink
Merge pull request #70 from inpsyde/feature/65
Browse files Browse the repository at this point in the history
Get rid of Neutron PHP Standard
  • Loading branch information
gmazzap committed Aug 31, 2023
2 parents f483017 + eb1a128 commit 91f94c9
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 167 deletions.
1 change: 1 addition & 0 deletions .github/workflows/quality-assurance-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- '**phpcs.xml.dist'
- '**phpunit.xml.dist'
- '**psalm.xml'
- '**ruleset.xml'
workflow_dispatch:
inputs:
jobs:
Expand Down
14 changes: 0 additions & 14 deletions .psalm/autoloader.php

This file was deleted.

3 changes: 1 addition & 2 deletions Inpsyde/PhpcsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ public static function isVoidReturn(
*/
public static function isNullReturn(File $file, int $returnPosition): bool
{
return
!self::isVoidReturn($file, $returnPosition, false)
return !self::isVoidReturn($file, $returnPosition, false)
&& self::isVoidReturn($file, $returnPosition, true);
}

Expand Down
48 changes: 48 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the "php-coding-standards" package.
*
* Copyright (C) 2023 Inpsyde GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

declare(strict_types=1);

namespace Inpsyde\Sniffs\CodeQuality;

use WordPressCS\WordPress\AbstractFunctionRestrictionsSniff;

class DisableCallUserFuncSniff extends AbstractFunctionRestrictionsSniff
{
/**
* @return array<string, array<string, string|array>>
*
* phpcs:disable Inpsyde.CodeQuality.NoAccessors
*/
public function getGroups(): array
{
// phpcs:enable Inpsyde.CodeQuality.NoAccessors
return [
'call_user_func' => [
'type' => 'error',
'message' => 'Usage of %s() is forbidden.',
'functions' => [
'call_user_func',
'call_user_func_array',
],
],
];
}
}
74 changes: 74 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* This file is part of the "php-coding-standards" package.
*
* Copyright (C) 2023 Inpsyde GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

declare(strict_types=1);

namespace Inpsyde\Sniffs\CodeQuality;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\Scopes;

class DisableMagicSerializeSniff implements Sniff
{
/** @var list<string> */
public array $disabledFunctions = [
'__serialize',
'__sleep',
'__unserialize',
'__wakeup',
];

/**
* @return list<int>
*/
public function register(): array
{
return [T_FUNCTION];
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @return void
*
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration
*/
public function process(File $phpcsFile, $stackPtr)
{
// phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration
if (!Scopes::isOOMethod($phpcsFile, $stackPtr)) {
return;
}

$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
if (in_array($name, $this->disabledFunctions, true)) {
$phpcsFile->addError(
sprintf(
'The method "%s" is forbidden, please use Serializable interface.',
$name
),
$stackPtr,
'Found'
);
}
}
}
71 changes: 71 additions & 0 deletions Inpsyde/Sniffs/CodeQuality/NoRootNamespaceFunctionsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This file is part of the "php-coding-standards" package.
*
* Copyright (C) 2023 Inpsyde GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

declare(strict_types=1);

namespace Inpsyde\Sniffs\CodeQuality;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHPCSUtils\Utils\FunctionDeclarations;
use PHPCSUtils\Utils\Namespaces;
use PHPCSUtils\Utils\ObjectDeclarations;
use PHPCSUtils\Utils\Scopes;

class NoRootNamespaceFunctionsSniff implements Sniff
{
/**
* @return list<int>
*/
public function register(): array
{
return [T_FUNCTION];
}

/**
* @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
if (Scopes::isOOMethod($phpcsFile, $stackPtr)) {
return;
}

$namespace = Namespaces::determineNamespace($phpcsFile, $stackPtr);
if ($namespace !== '') {
return;
}
$name = FunctionDeclarations::getName($phpcsFile, $stackPtr);
if (!$name) {
return;
}

$message = sprintf('The function "%s" is in root namespace.', $name);

$phpcsFile->addError($message, $stackPtr, 'Found');
}
}
80 changes: 49 additions & 31 deletions Inpsyde/ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<ruleset name="Inpsyde Coding Standard">
<ruleset name="Inpsyde PHP Coding Standard">

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

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

<!--
Neutron standard are quality tools for PHP7 development from Automattic.
See https://github.com/Automattic/phpcs-neutron-standard
-->
<rule ref="NeutronStandard.AssignAlign.DisallowAssignAlign">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.Functions.DisallowCallUserFunc">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.Globals.DisallowGlobalFunctions">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.MagicMethods.DisallowMagicSerialize">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.StrictTypes.RequireStrictTypes">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.Whitespace.DisallowMultipleNewlines">
<type>warning</type>
</rule>
<rule ref="NeutronStandard.Whitespace.RequireNewlineBetweenFunctions">
<type>warning</type>
</rule>

<!--
Curated list of WordPress specific rules.
See https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
-->
<rule ref="WordPress.Arrays.CommaAfterArrayItem"/>
<rule ref="WordPress.CodeAnalysis.AssignmentInCondition"/>
<rule ref="WordPress.CodeAnalysis.EmptyStatement"/>
<rule ref="WordPress.CodeAnalysis.AssignmentInTernaryCondition"/>
<rule ref="WordPress.CodeAnalysis.EscapedNotTranslated"/>
<rule ref="WordPress.DB.PreparedSQLPlaceholders"/>
<rule ref="WordPress.DB.PreparedSQL"/>
<rule ref="WordPress.DB.RestrictedClasses"/>
<rule ref="WordPress.DB.RestrictedFunctions"/>
<rule ref="WordPress.DateTime.CurrentTimeTimestamp"/>
<rule ref="WordPress.DateTime.RestrictedFunctions">
<properties>
<property name="exclude" type="array" value="date"/>
</properties>
</rule>
<rule ref="WordPress.NamingConventions.PrefixAllGlobals"/>
<rule ref="WordPress.NamingConventions.ValidHookName">
<properties>
Expand Down Expand Up @@ -81,7 +58,6 @@
<rule ref="WordPress.PHP.POSIXFunctions"/>
<rule ref="WordPress.PHP.PregQuoteDelimiter"/>
<rule ref="WordPress.PHP.RestrictedPHPFunctions"/>
<rule ref="WordPress.PHP.StrictComparisons"/>
<rule ref="WordPress.PHP.StrictInArray"/>
<rule ref="WordPress.PHP.TypeCasts"/>
<rule ref="WordPress.Security.EscapeOutput"/>
Expand Down Expand Up @@ -131,10 +107,38 @@
<exclude name="VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable" />
</rule>

<!--
PHPCS Extra
-->
<rule ref="NormalizedArrays.Arrays.CommaAfterLast" />
<rule ref="Universal.Operators.StrictComparisons" />
<rule ref="Universal.WhiteSpace.PrecisionAlignment" />

<!--
Slevomat
-->
<rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration">
<properties>
<property name="allowMultiLine" type="boolean" value="yes"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="spacesCountAroundEqualsSign" type="integer" value="0"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces">
<properties>
<property type="boolean" name="ignoreSpacesInComment" value="yes" />
</properties>
</rule>

<!--
Generic
-->
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
<rule ref="Generic.CodeAnalysis.AssignmentInCondition"/>
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
<rule ref="Generic.Metrics.CyclomaticComplexity">
<properties>
<property name="absoluteComplexity" value="50"/>
Expand All @@ -144,6 +148,13 @@
<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag"/>
<rule ref="Generic.PHP.LowerCaseConstant"/>
<rule ref="Generic.VersionControl.GitMergeConflict"/>
<rule ref="Generic.WhiteSpace.ArbitraryParenthesesSpacing">
<properties>
<property type="boolean" name="ignoreNewlines" value="yes" />
</properties>
</rule>
<rule ref="Generic.WhiteSpace.LanguageConstructSpacing"/>
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>
<rule ref="Squiz.Classes.LowercaseClassKeywords"/>
<rule ref="Squiz.PHP.CommentedOutCode">
<properties>
Expand All @@ -156,6 +167,13 @@
<rule ref="Squiz.PHP.NonExecutableCode"/>
<rule ref="Squiz.Scope.MemberVarScope"/>
<rule ref="Squiz.Scope.StaticThisUsage"/>
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
<properties>
<property name="spacing" value="1"/>
<property name="spacingBeforeFirst" value="0"/>
<property name="spacingAfterLast" value="0"/>
</properties>
</rule>

<!--
PHPCompatibility
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
"minimum-stability": "stable",
"require": {
"php": ">=7.4",
"dealerdirect/phpcodesniffer-composer-installer": "~1.0.0",
"automattic/vipwpcs": "dev-develop",
"dealerdirect/phpcodesniffer-composer-installer": "~1.0.0",
"phpcompatibility/php-compatibility": "^9.3.5",
"automattic/phpcs-neutron-standard": "^v1.7.0"
"phpcsstandards/phpcsextra": "^1.1",
"phpcsstandards/phpcsutils": "^1.0",
"slevomat/coding-standard": "^8.13"
},
"require-dev": {
"phpunit/phpunit": "^9.6.11",
Expand Down Expand Up @@ -65,6 +67,7 @@
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
"inpsyde/*": true,
"composer/*": true,
Expand Down
Loading

0 comments on commit 91f94c9

Please sign in to comment.