Skip to content

Commit f9ba1bd

Browse files
committed
Require the usage of fully-qualified global functions
1 parent 851c294 commit f9ba1bd

14 files changed

+57
-72
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with some noticeable exceptions/differences/extensions based on best-practices a
2626
- Use parentheses when creating new instances that do not require arguments ``$foo = new Foo()``
2727
- Use Null Coalesce Operator ``$foo = $bar ?? $baz``
2828
- Prefer early exit over nesting conditions or using else
29+
- Always use fully-qualified global functions (without needing `use function` statements)
2930

3031
For full reference of enforcements, go through ``src/Unleashed/ruleset.xml`` where each sniff is briefly described.
3132

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"php": "^7.1",
3030
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
3131
"slevomat/coding-standard": "^6.2.0",
32-
"squizlabs/php_codesniffer": "^3.4.0"
32+
"squizlabs/php_codesniffer": "^3.4.0",
33+
"soderlind/coding-standard": "^0.0.7"
3334
},
3435
"scripts": {
3536
"test": [

src/Unleashed/ruleset.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@
284284
<properties>
285285
<property name="allowFullyQualifiedExceptions" value="true"/>
286286
<property name="allowFullyQualifiedGlobalClasses" value="true"/>
287-
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
288-
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
287+
<property name="allowFullyQualifiedGlobalConstants" value="true"/>
288+
<property name="allowFullyQualifiedGlobalFunctions" value="true"/>
289289
<property name="searchAnnotations" value="true"/>
290290
</properties>
291291
</rule>
292+
<!-- Force usage of fully-qualified global functions -->
293+
<rule ref="FullyQualifiedGlobalFunctions"/>
292294
<!-- Forbid unused use statements -->
293295
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
294296
<properties>

tests/fixed/UselessConditions.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Conditions;
66

7-
use function count;
8-
use function strpos;
9-
107
class UselessConditions
118
{
129
public function uselessCondition(): bool
@@ -124,11 +121,11 @@ public function uselessTernaryWithMethod(): bool
124121
*/
125122
public function uselessTernaryCheck(array $words): bool
126123
{
127-
return count($words) < 1;
124+
return \count($words) < 1;
128125
}
129126

130127
public function necessaryTernary(): int
131128
{
132-
return strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
129+
return \strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
133130
}
134131
}

tests/fixed/example-class.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
use Fancy\TestCase;
99

1010
use function strlen as stringLength;
11-
use function substr;
1211

1312
use const PHP_RELEASE_VERSION as PHP_PATCH_VERSION;
14-
use const PHP_VERSION;
1513

1614
/**
1715
* Description
1816
*/
1917
class Example implements \IteratorAggregate
2018
{
21-
private const VERSION = PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
19+
private const VERSION = \PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
2220

2321
/** @var int|null */
2422
private $foo;
@@ -53,7 +51,7 @@ public function getFoo(): ?int
5351
*/
5452
public function getIterator(): array
5553
{
56-
assert($this->bar !== null);
54+
\assert($this->bar !== null);
5755

5856
return new \ArrayIterator($this->bar);
5957
}
@@ -74,7 +72,7 @@ public function mangleBar(int $length): void
7472
throw new \InvalidArgumentException();
7573
}
7674

77-
$this->bar = (string) $this->baxBax ?? substr($this->bar, stringLength($this->bar - $length));
75+
$this->bar = (string) $this->baxBax ?? \substr($this->bar, stringLength($this->bar - $length));
7876
}
7977

8078
public static function getMinorVersion(): int

tests/fixed/forbidden-functions.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,19 @@
44

55
namespace Test;
66

7-
use function chop;
8-
use function compact;
9-
use function extract;
10-
use function is_null;
11-
use function settype;
12-
use function sizeof;
13-
use function var_dump;
14-
15-
echo chop('abc ');
16-
echo sizeof([1, 2, 3]);
17-
echo is_null(456) ? 'y' : 'n';
7+
echo \chop('abc ');
8+
echo \sizeof([1, 2, 3]);
9+
echo \is_null(456) ? 'y' : 'n';
1810

1911
$foo = '1';
20-
settype($foo, 'int');
21-
var_dump($foo);
12+
\settype($foo, 'int');
13+
\var_dump($foo);
2214

2315
$bar = [
2416
'foo' => 1,
2517
'bar' => 2,
2618
'baz' => 3,
2719
];
28-
extract($bar);
20+
\extract($bar);
2921

30-
compact('foo', 'bar');
22+
\compact('foo', 'bar');

tests/fixed/fully-qualified-and-fallbacks.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
use DateInterval;
88

9-
use function strrev;
109
use function time as now;
1110

1211
use const DATE_RFC3339;
1312

14-
strrev(strrev(
13+
\strrev(\strrev(
1514
(new \DateTimeImmutable('@' . now(), new DateTimeZone('UTC')))
1615
->sub(new DateInterval('P1D'))
1716
->format(DATE_RFC3339)

tests/fixed/namespaces-spacing.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
use DateTimeImmutable;
99
use DateTimeZone;
1010

11-
use function strrev;
12-
use function time;
11+
use function time as now;
1312

1413
use const DATE_RFC3339;
1514

16-
strrev(
17-
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
15+
\strrev(
16+
(new DateTimeImmutable('@' . now(), new DateTimeZone('UTC')))
1817
->sub(new DateInterval('P1D'))
1918
->format(DATE_RFC3339)
2019
);

tests/fixed/use-ordering.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use DateTimeImmutable;
88
use DateTimeInterface;
99

10-
use function sprintf;
10+
use function sprintf as s;
1111

1212
use const PHP_EOL;
1313

14-
echo sprintf('Current date and time is %s', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)) . PHP_EOL;
14+
echo s('Current date and time is %s', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)) . PHP_EOL;

tests/input/UselessConditions.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Conditions;
66

7-
use function count;
8-
use function strpos;
9-
107
class UselessConditions
118
{
129
public function uselessCondition(): bool
@@ -156,11 +153,11 @@ public function uselessTernaryWithMethod(): bool
156153
*/
157154
public function uselessTernaryCheck(array $words) : bool
158155
{
159-
return count($words) >= 1 ? false : true;
156+
return \count($words) >= 1 ? false : true;
160157
}
161158

162159
public function necessaryTernary(): int
163160
{
164-
return strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
161+
return \strpos('foo', 'This is foo and bar') !== false ? 1 : 0;
165162
}
166163
}

tests/input/forbidden-functions.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,19 @@
44

55
namespace Test;
66

7-
use function chop;
8-
use function compact;
9-
use function extract;
10-
use function is_null;
11-
use function settype;
12-
use function sizeof;
13-
use function var_dump;
14-
15-
echo chop('abc ');
16-
echo sizeof([1, 2, 3]);
17-
echo is_null(456) ? 'y' : 'n';
7+
echo \chop('abc ');
8+
echo \sizeof([1, 2, 3]);
9+
echo \is_null(456) ? 'y' : 'n';
1810

1911
$foo = '1';
20-
settype($foo, 'int');
21-
var_dump($foo);
12+
\settype($foo, 'int');
13+
\var_dump($foo);
2214

2315
$bar = [
2416
'foo' => 1,
2517
'bar' => 2,
2618
'baz' => 3,
2719
];
28-
extract($bar);
20+
\extract($bar);
2921

30-
compact('foo', 'bar');
22+
\compact('foo', 'bar');

tests/input/namespaces-spacing.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
use DateInterval;
66
use DateTimeImmutable;
77
use DateTimeZone;
8-
use function strrev;
9-
use function time;
8+
use function time as now;
109
use const DATE_RFC3339;
11-
strrev(
12-
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
10+
\strrev(
11+
(new DateTimeImmutable('@' . now(), new DateTimeZone('UTC')))
1312
->sub(new DateInterval('P1D'))
1413
->format(DATE_RFC3339)
1514
);

tests/input/use-ordering.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Foo;
66

7-
use function sprintf;
7+
use function sprintf as s;
88
use DateTimeImmutable;
99
use const PHP_EOL;
1010
use DateTimeInterface;
1111

12-
echo sprintf('Current date and time is %s', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)) . PHP_EOL;
12+
echo s('Current date and time is %s', (new DateTimeImmutable())->format(DateTimeInterface::ATOM)) . PHP_EOL;

tests/php-compatibility.patch

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 1ecfaaa..d18e990 100644
2+
index 1ecfaaa..262b310 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
5-
@@ -11,40 +11,40 @@ tests/input/concatenation_spacing.php 49 0
5+
@@ -6,45 +6,45 @@ FILE ERRORS WARNINGS
6+
tests/input/array_indentation.php 10 0
7+
tests/input/assignment-operators.php 4 0
8+
tests/input/binary_operators.php 9 0
9+
-tests/input/class-references.php 10 0
10+
+tests/input/class-references.php 15 0
11+
tests/input/concatenation_spacing.php 49 0
612
tests/input/constants-no-lsb.php 2 0
713
tests/input/constants-var.php 4 0
814
tests/input/ControlStructures.php 17 2
@@ -11,24 +17,26 @@ index 1ecfaaa..d18e990 100644
1117
tests/input/duplicate-assignment-variable.php 1 0
1218
tests/input/EarlyReturn.php 6 0
1319
-tests/input/example-class.php 38 0
14-
+tests/input/example-class.php 42 0
20+
+tests/input/example-class.php 41 0
1521
tests/input/forbidden-comments.php 14 0
1622
tests/input/forbidden-functions.php 5 0
1723
tests/input/fully-qualified-and-fallbacks.php 1 0
18-
tests/input/inline_type_hint_assertions.php 7 0
24+
-tests/input/inline_type_hint_assertions.php 7 0
1925
-tests/input/LowCaseTypes.php 4 0
2026
-tests/input/namespaces-spacing.php 8 0
2127
-tests/input/NamingCamelCase.php 9 0
28+
+tests/input/inline_type_hint_assertions.php 8 0
2229
+tests/input/LowCaseTypes.php 3 0
2330
+tests/input/namespaces-spacing.php 12 0
2431
+tests/input/NamingCamelCase.php 8 0
2532
tests/input/negation-operator.php 2 0
2633
tests/input/new_with_parentheses.php 18 0
2734
tests/input/not_spacing.php 6 0
2835
tests/input/null_coalesce_operator.php 3 0
29-
tests/input/optimized-functions.php 1 0
36+
-tests/input/optimized-functions.php 1 0
3037
-tests/input/return_type_on_closures.php 29 0
3138
-tests/input/return_type_on_methods.php 25 0
39+
+tests/input/optimized-functions.php 2 0
3240
+tests/input/return_type_on_closures.php 17 0
3341
+tests/input/return_type_on_methods.php 13 0
3442
tests/input/semicolon_spacing.php 3 0
@@ -50,20 +58,20 @@ index 1ecfaaa..d18e990 100644
5058
+tests/input/UselessConditions.php 23 0
5159
----------------------------------------------------------------------
5260
-A TOTAL OF 388 ERRORS AND 2 WARNINGS WERE FOUND IN 38 FILES
53-
+A TOTAL OF 356 ERRORS AND 2 WARNINGS WERE FOUND IN 38 FILES
61+
+A TOTAL OF 362 ERRORS AND 2 WARNINGS WERE FOUND IN 38 FILES
5462
----------------------------------------------------------------------
5563
-PHPCBF CAN FIX 331 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
56-
+PHPCBF CAN FIX 299 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
64+
+PHPCBF CAN FIX 305 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5765
----------------------------------------------------------------------
5866

5967

6068
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
61-
index 06efb7e..881afd3 100644
69+
index 9df3d20..108bb03 100644
6270
--- a/tests/fixed/example-class.php
6371
+++ b/tests/fixed/example-class.php
64-
@@ -20,14 +20,12 @@ class Example implements \IteratorAggregate
72+
@@ -18,14 +18,12 @@ class Example implements \IteratorAggregate
6573
{
66-
private const VERSION = PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
74+
private const VERSION = \PHP_VERSION - (PHP_MINOR_VERSION * 100) - PHP_PATCH_VERSION;
6775

6876
- /** @var int|null */
6977
- private $foo;

0 commit comments

Comments
 (0)