Skip to content
This repository was archived by the owner on Nov 21, 2019. It is now read-only.
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": "^7.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4",
"slevomat/coding-standard": "^4.6.1",
"slevomat/coding-standard": "^4.8",
"squizlabs/php_codesniffer": "^3.3.2"
},
"require-dev": {
Expand Down
7 changes: 7 additions & 0 deletions src/Libero/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@
<property name="psr12Compatible" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFallbackGlobalConstants" value="false"/>
<property name="allowFallbackGlobalFunctions" value="false"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
Expand Down
1 change: 1 addition & 0 deletions tests/RulesetTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use function implode;
use function ini_get;
use function mb_convert_encoding;
use function mb_detect_encoding;
use function preg_match_all;
use function preg_replace;
use function sort;
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/php/global-fallbacks
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---DESCRIPTION---
No global fallbacks
---CONTENTS---
<?php

declare(strict_types=1);

namespace Vendor;

date(DATE_ATOM);

---FIXED---
<?php

declare(strict_types=1);

namespace Vendor;

use function date;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this becoming annoying for many PHP functions e.g. all strlen and similar which gets used everywhere. Even Java auto-import java.lang to avoid too many imports. PHP provides the fallback too for functions (but not for classes IIRC).

If anything I would forbid defining a strlen that is not \strlen, which tries to avoid the ambiguity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some functions get a very-minor performance boost by not checking the fallback. But more importantly prevents https://www.giorgiosironi.com/2010/09/monkey-patching-in-php.html (as used by Symfony's ClockMock).

Might be annoying when not using an IDE though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use const DATE_ATOM;

date(DATE_ATOM);

---
32 changes: 32 additions & 0 deletions tests/cases/php/use-fully-qualified
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---DESCRIPTION---
Use statements must be used

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for classes, this is expected

---CONTENTS---
<?php

declare(strict_types=1);

namespace Vendor;

new \Foo(\bar(\BAZ));
new \Qux\Quux(\Qux\quuz(\Qux\CORGE));
new \Vendor\Grault(\Vendor\garply(\Vendor\WALDO));

---FIXED---
<?php

declare(strict_types=1);

namespace Vendor;

use Foo;
use Qux\Quux;
use function bar;
use function Qux\quuz;
use const BAZ;
use const Qux\CORGE;

new Foo(bar(BAZ));
new Quux(quuz(CORGE));
new Grault(garply(WALDO));

---
25 changes: 25 additions & 0 deletions tests/cases/php/use-useless
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---DESCRIPTION---
Use statements must not be from the same namespace

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good practice

---CONTENTS---
<?php

declare(strict_types=1);

namespace Foo;

use Foo\Bar;
use function Foo\baz;
use const Foo\QUX;

new Bar(baz(QUX));

---FIXED---
<?php

declare(strict_types=1);

namespace Foo;

new Bar(baz(QUX));

---
4 changes: 2 additions & 2 deletions tests/cases/whitespace/use-namespace
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Use declarations must go after the namespace declaration

declare(strict_types=1);

use Foo;
use Foo\Bar;

namespace Vendor;

new Foo();
new Bar();

---MESSAGES---
5:1 PSR2.Namespaces.UseDeclaration.UseBeforeNamespace
Expand Down