Skip to content

Commit 9111541

Browse files
authored
Merge pull request #1 from ProtonMail/feat/namespaced-function-sniff
Create new Sniff to check namespace before reporting forbidden function
2 parents e9b771d + 51784f5 commit 9111541

File tree

7 files changed

+70
-23
lines changed

7 files changed

+70
-23
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Sniffs\CodingStandard;
6+
7+
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
8+
use SlevomatCodingStandard\Helpers\NamespaceHelper;
9+
10+
class ForbiddenNamespacedFunctionsSniff extends ForbiddenFunctionsSniff
11+
{
12+
protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
13+
{
14+
$fqfn = NamespaceHelper::resolveName($phpcsFile, $function, 'function', $stackPtr);
15+
$canonicalName = NamespaceHelper::normalizeToCanonicalName($fqfn);
16+
17+
$currentNS = NamespaceHelper::findCurrentNamespaceName($phpcsFile, $stackPtr);
18+
$canonicalFunction = NamespaceHelper::normalizeToCanonicalName($currentNS . '\\' . $function);
19+
20+
if ($canonicalName === $canonicalFunction) {
21+
parent::addError($phpcsFile, $stackPtr, $function, $pattern);
22+
}
23+
}
24+
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"minimum-stability": "dev",
1212
"prefer-stable": true,
1313
"scripts": {
14-
"test-print": "phpcs --standard=Proton/ruleset.xml --report=summary tests",
15-
"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"
14+
"test-print": "phpcs --standard=tests/phpcs.xml --report=summary tests",
15+
"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"
1616
},
1717
"require": {
1818
"php": "^7.1",

tests/correct/ClassOk.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Proton\Test;
66

77
use Proton\Http\Request;
8+
use function Proton\Support\phpinfo2;
89

910
final class ClassOk
1011
{
@@ -21,6 +22,8 @@ public function __construct(array $config)
2122
$this->foo = $config + [
2223
'foo' => 'bar',
2324
];
25+
26+
phpinfo2();
2427
}
2528

2629
public function ping(Request $request)

tests/expected.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ PHP CODE SNIFFER REPORT SUMMARY
33
------------------------------------------------------------------------------------------------
44
FILE ERRORS WARNINGS
55
------------------------------------------------------------------------------------------------
6-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 30 0
7-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 0
6+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 31 3
7+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
88
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
99
------------------------------------------------------------------------------------------------
10-
A TOTAL OF 36 ERRORS AND 0 WARNINGS WERE FOUND IN 3 FILES
10+
A TOTAL OF 37 ERRORS AND 5 WARNINGS WERE FOUND IN 3 FILES
11+
------------------------------------------------------------------------------------------------
12+
PHPCBF CAN FIX 29 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
1113
------------------------------------------------------------------------------------------------
12-
PHPCBF CAN FIX 28 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
13-
------------------------------------------------------------------------------------------------

tests/expected_csv.txt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,27 @@ Line,Column,Type,Source
99
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
1010
8,1,error, found 1."
1111
6,18,error,PSR12.Files.FileHeader.SpacingInsideBlock
12-
40,1,error,PSR2.Files.EndFileNewline.NoneFound
12+
41,1,error,PSR2.Files.EndFileNewline.NoneFound
1313
4,1,error, found 2."
14+
39,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
15+
38,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
1416
38,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
15-
37,13,warning,Squiz.PHP.NonExecutableCode.Unreachable
1617
37,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
18+
36,9,error,Generic.PHP.LowerCaseKeyword.Found
19+
36,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
20+
36,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
21+
36,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
1722
36,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
18-
35,9,error,Generic.PHP.LowerCaseKeyword.Found
19-
35,9,error,Squiz.ControlStructures.LowercaseDeclaration.FoundUppercase
20-
35,9,error,Squiz.ControlStructures.ControlSignature.SpaceAfterKeyword
21-
35,12,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
22-
35,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
23-
33,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
24-
33,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
25-
33,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
26-
33,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
27-
33,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
28-
31,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
29-
31,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
30-
27,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
31-
24,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
23+
34,9,error,Squiz.NamingConventions.ValidVariableName.NotCamelCaps
24+
34,24,warning,Generic.CodeAnalysis.EmptyPHPStatement.SemicolonWithoutCodeDetected
25+
34,24,error,SlevomatCodingStandard.PHP.UselessSemicolon.UselessSemicolon
26+
34,24,error,Generic.Formatting.DisallowMultipleStatements.SameLine
27+
34,1,error,Generic.WhiteSpace.DisallowTabIndent.TabsUsed
28+
32,31,error,Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
29+
32,13,error,SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
30+
28,27,error,PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword
31+
25,13,error,SlevomatCodingStandard.Arrays.TrailingArrayComma.MissingTrailingComma
32+
22,9,error,Proton.CodingStandard.ForbiddenNamespacedFunctions.FoundWithAlternative
3233
20,19,error,Generic.PHP.BacktickOperator.Found
3334
20,14,error,Generic.PHP.BacktickOperator.Found
3435
16,5,error,PSR12.Traits.UseDeclaration.UseAfterBrace

tests/phpcs.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="ProtonLabs PHP CodeSniffer Standard">
3+
<description>
4+
Proton PHP Coding Standard
5+
</description>
6+
7+
<config name="installed_paths" value="../Proton"/>
8+
9+
<rule ref="Proton"/>
10+
11+
<rule ref="Proton.CodingStandard.ForbiddenNamespacedFunctions">
12+
<properties>
13+
<property name="forbiddenFunctions" type="array">
14+
<element key="phpinfo2" value="\Proton\Support\phpinfo2"/>
15+
</property>
16+
</properties>
17+
</rule>
18+
</ruleset>

tests/wrong/Class1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct()
1919
{
2020
echo `test`;
2121

22+
phpinfo2();
2223

2324
print_r([
2425
1

0 commit comments

Comments
 (0)