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 1 commit
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
7 changes: 7 additions & 0 deletions src/Libero/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,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
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));

---
11 changes: 9 additions & 2 deletions tests/cases/whitespace/use-namespace
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace Vendor;

new Foo();

---MESSAGES---
5:1 PSR2.Namespaces.UseDeclaration.UseBeforeNamespace
---FIXED---
<?php

declare(strict_types=1);

namespace Vendor;

new Foo();
Copy link
Contributor Author

@thewilkybarkid thewilkybarkid Sep 25, 2018

Choose a reason for hiding this comment

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

This is wrong, as the use statement is being removed. One of the new sniffs must be misbehaving. Edit: It's UseFromSameNamespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at it, UseFromSameNamespace is actually correct because the use statement is in the global namespace, where it's not needed. Would still rather UseBeforeNamespace is triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can use a namespaced class to get round this for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Just checked what happens and the code's going to be broken either way.)


---