Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Proton/Sniffs/CodingStandard/ForbiddenNamespacedFunctionsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Proton\Sniffs\CodingStandard;

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use SlevomatCodingStandard\Helpers\NamespaceHelper;

class ForbiddenNamespacedFunctionsSniff extends ForbiddenFunctionsSniff
{
protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
{
$fqfn = NamespaceHelper::resolveName($phpcsFile, $function, 'function', $stackPtr);
$canonicalName = NamespaceHelper::normalizeToCanonicalName($fqfn);

$currentNS = NamespaceHelper::findCurrentNamespaceName($phpcsFile, $stackPtr);
$canonicalFunction = NamespaceHelper::normalizeToCanonicalName($currentNS . '\\' . $function);

if ($canonicalName === $canonicalFunction) {
parent::addError($phpcsFile, $stackPtr, $function, $pattern);
}
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test-print": "phpcs --standard=Proton/ruleset.xml --report=summary tests",
"test": "phpcs --standard=Proton/ruleset.xml --report=csv tests | sort -r | cut -f 2,3,4,6 -d ',' > tests/out.csv; diff tests/expected_csv.txt tests/out.csv"
"test-print": "phpcs --standard=tests/phpcs.xml --report=summary tests",
"test": "phpcs --standard=tests/phpcs.xml --report=csv tests | sort -r | cut -f 2,3,4,6 -d ',' > tests/out.csv; diff tests/expected_csv.txt tests/out.csv"
},
"require": {
"php": "^7.1",
Expand Down
3 changes: 3 additions & 0 deletions tests/correct/ClassOk.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Proton\Test;

use Proton\Http\Request;
use function Proton\Support\phpinfo2;

final class ClassOk
{
Expand All @@ -21,6 +22,8 @@ public function __construct(array $config)
$this->foo = $config + [
'foo' => 'bar',
];

phpinfo2();
}

public function ping(Request $request)
Expand Down
10 changes: 5 additions & 5 deletions tests/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ PHP CODE SNIFFER REPORT SUMMARY
------------------------------------------------------------------------------------------------
FILE ERRORS WARNINGS
------------------------------------------------------------------------------------------------
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 30 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 0
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 31 3
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
------------------------------------------------------------------------------------------------
A TOTAL OF 36 ERRORS AND 0 WARNINGS WERE FOUND IN 3 FILES
A TOTAL OF 37 ERRORS AND 5 WARNINGS WERE FOUND IN 3 FILES
------------------------------------------------------------------------------------------------
PHPCBF CAN FIX 29 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------
PHPCBF CAN FIX 28 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------
33 changes: 17 additions & 16 deletions tests/expected_csv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ Line,Column,Type,Source
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
8,1,error, found 1."
6,18,error,PSR12.Files.FileHeader.SpacingInsideBlock
40,1,error,PSR2.Files.EndFileNewline.NoneFound
41,1,error,PSR2.Files.EndFileNewline.NoneFound
4,1,error, found 2."
39,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
38,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
38,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
37,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
37,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
36,9,error,Generic.PHP.LowerCaseKeyword.Found
36,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
36,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
36,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
36,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
35,9,error,Generic.PHP.LowerCaseKeyword.Found
35,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
35,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
35,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
35,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
33,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
33,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
33,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
33,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
33,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
31,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
31,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
27,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
24,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
34,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
34,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
34,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
34,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
34,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
32,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
32,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
28,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
25,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
22,9,error,Proton.CodingStandard.ForbiddenNamespacedFunctions.FoundWithAlternative
20,19,error,Generic.PHP.BacktickOperator.Found
20,14,error,Generic.PHP.BacktickOperator.Found
16,5,error,PSR12.Traits.UseDeclaration.UseAfterBrace
Expand Down
18 changes: 18 additions & 0 deletions tests/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<ruleset name="ProtonLabs PHP CodeSniffer Standard">
<description>
Proton PHP Coding Standard
</description>

<config name="installed_paths" value="../Proton"/>

<rule ref="Proton"/>

<rule ref="Proton.CodingStandard.ForbiddenNamespacedFunctions">
<properties>
<property name="forbiddenFunctions" type="array">
<element key="phpinfo2" value="\Proton\Support\phpinfo2"/>
</property>
</properties>
</rule>
</ruleset>
1 change: 1 addition & 0 deletions tests/wrong/Class1.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct()
{
echo `test`;

phpinfo2();

print_r([
1
Expand Down