slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.39k stars 176 forks source link

Fix for ReferenceViaFullyQualifiedNameWithoutNamespace is wrong #344

Closed SebastianS90 closed 6 years ago

SebastianS90 commented 6 years ago

SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedNameWithoutNamespace

Class \Exception should not be referenced via a fully qualified name, but via an unqualified name without the leading \, because the file does not have a namespace and the type cannot be put in a use statement.

The fix does:

 /*
- * @throws \Exception
+ * throws \Exception
  */

But it should:

 /*
- * @throws \Exception
+ * @throws Exception
  */
kukulich commented 6 years ago

@SebastianS90 Please post full example. Your code should not be checked because there is no doccomment.

SebastianS90 commented 6 years ago

phpcs.xml

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <config name="installed_paths" value="../../slevomat/coding-standard"/>
    <rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
        <properties>
            <property name="searchAnnotations" value="1"/>
        </properties>
    </rule>
</ruleset>

foo.php

<?php

/**
 * Just a small test.
 *
 * @throws \Exception
 */
function foo(): void
{
    throw new Exception();
}

$ phpcs foo.php

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 6 | ERROR | [x] Class \Exception should not be referenced via a fully qualified name, but via an unqualified name without the leading \, because the file does not have a namespace and the type cannot be put
   |       |     in a use statement.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

$ phpcbf foo.php

PHPCBF RESULT SUMMARY
-----------------------------------------------------------------------
FILE                                                   FIXED  REMAINING
-----------------------------------------------------------------------
foo.php                                                1      0
-----------------------------------------------------------------------
A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE
-----------------------------------------------------------------------

foo.php

<?php

/**
 * Just a small test.
 *
 * throws \Exception
 */
function foo(): void
{
    throw new Exception();
}

$ git diff

diff --git a/foo.php b/foo.php
--- a/foo.php
+++ b/foo.php
@@ -3,7 +3,7 @@
 /**
  * Just a small test.
  *
- * @throws \Exception
+ * throws \Exception
  */
 function foo(): void
 {
kukulich commented 6 years ago

Thank you for your report. Fixed in https://github.com/slevomat/coding-standard/commit/2b5a5a2768f058fd77cb069974b7fe9427cae94c

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.