27 Jul 2016

Introducing new rules for PHP 7 and Symfony

A few months ago, we added the support for PHP 7 projects in SensioLabsInsight. Today, we are glad to announce the addition of several new rules concerning PHP 7 and Symfony.

New rule for missing visibillity on class methods and properties

One of the main goals of SensioLabsInsight is to promote the best practices used by professional PHP applications. With this in mind, we introduced a new rule to ensure a project always uses visibility flags (public/protected/private) on its class methods and properties.

There are a few reasons to always use visibility flags:

  • it helps to show the intent of classes
  • it provides coding guidelines on how the code should be used
  • it reduces the need to validate properties when used internally
class Example
{
    // This will now trigger a violation
    static $foo = 4;

    // This will now trigger a violation
    function bar() {
        return 0;
    }

    // This is valid
    public static $foob = 4;

    // This is valid
    private function barb() {
        return 0;
    }
}

New rule for PHP 7 reserved words

PHP 7 introduced a list of new reserved keywords (and PHP 7.1 is continuing to do so) which should not be used as classes, interfaces or traits names to avoid issues when migrating.

This rule will warn you if a class, an interface or a trait in your code uses a reserved keyword.

// Invalid class name
class Int {}

// Invalid interface name
interface Bool {}

// Invalid trait name
trait Void {}

Return type hints are now properly taken into account

Until now, return type hints were not taken into account as the usage of a class or an interface and false positive "Unused use statement" violations were triggered.

This is fixed as return type hints are now handled properly by SensioLabsInsight.

use Foo\Bar;

// This won't trigger a "Unused use statement" violation anymore
function myFunction(): Bar {
    return otherFunction();
}

Allow the usage of the Event annotation

The usage of the Event annotation in Symfony core code is progressively increasing to provide a useful autocompletion basis for development environments.

Until now, this annotation was considered as a regular Doctrine annotation and a violation was triggered as it was not imported with a use. We fixed this behavior to accept this annotation as a native one (on the same level as @param and @return).

Updated deprecated functions, classes and methods for PHP 7 and Symfony 3.2

Following the changes of PHP and Symfony in term of deprecations, we updated our rules to ease your work in migrating towards new versions.

comments powered by Disqus