Skip to content

Latest commit

 

History

History
507 lines (376 loc) · 9.44 KB

rules_overview.md

File metadata and controls

507 lines (376 loc) · 9.44 KB

8 Rules Overview

DisallowMethodCallOnUnsafeDataObjectRule

Call exists() first before accessing any magic SilverStripe\ORM\DataObject methods as the object may not be present in the database. Database manipulation methods such as write() and delete() are allowed by default. If you think a method is safe to call by default add it to the allowedMethodCalls configuration.

🔧 configure it!

parameters:
    silverstan:
        disallowMethodCallOnUnsafeDataObject:
            enabled: true
            allowedMethodCalls:
                - mySafeMethod

/**
 * @method \SilverStripe\ORM\DataObject Bar()
 */
final class Foo extends \SilverStripe\ORM\DataObject
{
    public function doSomething(): string
    {
        return $this->Bar()->doSomething();
    }
}


/**
 * @method \SilverStripe\ORM\DataObject Bar()
 */
final class Foo extends \SilverStripe\ORM\DataObject
{
    public function doSomething(): string
    {
        if (!$this->Bar()->exists()) {
            return '';
        }

        return $this->Bar()->doSomething();
    }
}

👍


DisallowNewInstanceOnInjectableRule

Disallow instantiating a SilverStripe\Core\Injectable class using new. Use create() instead.

🔧 configure it!

parameters:
    silverstan:
        disallowNewInstanceOnInjectable:
            enabled: true

final class Foo
{
    use \SilverStripe\Core\Injectable;
}

$foo = new Foo();


final class Foo
{
    use \SilverStripe\Core\Injectable;
}

$foo = Foo::create();

👍


DisallowOverridingOfConfigurationPropertyTypeRule

Disallow overriding types of configuration properties.

🔧 configure it!

parameters:
    silverstan:
        disallowOverridingOfConfigurationPropertyType:
            enabled: true

namespace App\Model;

class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $foo = 'foo';
}

final class Bar extends Foo
{
    private static string|bool $foo = false;
}


namespace App\Model;

class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $foo = 'foo';
}

final class Bar extends Foo
{
    private static string $foo = 'bar';
}

👍


DisallowPropertyFetchOnConfigForClassRule

Disallow property fetch on SilverStripe\Core\Config\Config_ForClass. PHPStan cannot resolve the type of the property, use self::config()->get('property_name') instead.

🔧 configure it!

parameters:
    silverstan:
        disallowPropertyFetchOnConfigForClass:
            enabled: true

final class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $singular_name = 'Foo';

    public function getType(): string
    {
        return self::config()->singular_name;
    }
}


final class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $singular_name = 'Foo';

    public function getType(): string
    {
        return self::config()->get('singular_name');
    }
}

👍


DisallowPropertyFetchOnUnsafeDataObjectRule

Call exists() first before accessing any magic SilverStripe\ORM\DataObject properties as the object may not be present in the database. Property assignment is allowed.

🔧 configure it!

parameters:
    silverstan:
        disallowPropertyFetchOnUnsafeDataObject:
            enabled: true

/**
 * @method \SilverStripe\ORM\DataObject Bar()
 */
final class Foo extends \SilverStripe\ORM\DataObject
{
    public function doSomething(): string
    {
        return $this->Bar()->Title;
    }
}


/**
 * @method \SilverStripe\ORM\DataObject Bar()
 */
final class Foo extends \SilverStripe\ORM\DataObject
{
    public function doSomething(): string
    {
        if (!$this->Bar()->exists()) {
            return '';
        }

        return $this->Bar()->Title;
    }
}

👍


DisallowStaticPropertyFetchOnConfigurationPropertyRule

Disallow static property fetch on configuration properties.

🔧 configure it!

parameters:
    silverstan:
        disallowStaticPropertyFetchOnConfigurationProperty:
            enabled: true

final class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $singular_name = 'Foo';

    public function getType(): string
    {
        return self::$singular_name;
    }
}


final class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $singular_name = 'Foo';

    public function getType(): string
    {
        return self::config()->get('singular_name');
    }
}

👍


RequireConfigurationPropertyOverrideRule

Require a class to override a set of configuration properties.

🔧 configure it!

parameters:
    silverstan:
        requireConfigurationPropertyOverride:
            enabled: true
            classes:
                -
                    class: SilverStripe\ORM\DataObject
                    properties:
                        - table_name

final class Foo extends \SilverStripe\ORM\DataObject
{
}


final class Foo extends \SilverStripe\ORM\DataObject
{
    private static string $table_name = 'Foo';
}

👍


RequireParentCallInOverridenMethodRule

Require parent call in an overriden method.

🔧 configure it!

parameters:
    silverstan:
        requireParentCallInOverridenMethod:
            enabled: true
            classes:
                -
                    class: SilverStripe\ORM\DataObject
                    method: onBeforeWrite
                -
                    class: SilverStripe\ORM\DataObject
                    method: onAfterWrite
                -
                    class: SilverStripe\ORM\DataObject
                    method: requireDefaultRecords
                -
                    class: SilverStripe\Dev\SapphireTest
                    method: setUp
                    isFirst: true
                -
                    class: SilverStripe\Dev\SapphireTest
                    method: setUpBeforeClass
                    isFirst: true
                -
                    class: SilverStripe\Dev\SapphireTest
                    method: tearDown
                -
                    class: SilverStripe\Dev\SapphireTest
                    method: tearDownAfterClass

namespace App\Model;

final class Foo extends \SilverStripe\ORM\DataObject
{
    protected function onBeforeWrite(): void
    {
        // Custom code...
    }

    protected function onAfterWrite(): void
    {
        // Custom code...
    }

    public function requireDefaultRecords(): void
    {
        // Custom code...
    }
}

namespace App\Tests\Model;

final class FooTest extends \SilverStripe\Dev\SapphireTest
{
    protected function setUp(): void
    {
        // Custom code...
    }

    protected function setUpBeforeClass(): void
    {
        // Custom code...
    }

    protected function tearDown(): void
    {
        // Custom code...
    }

    protected function tearDownAfterClass(): void
    {
        // Custom code...
    }
}


namespace App\Model;

final class Foo extends \SilverStripe\ORM\DataObject
{
    protected function onBeforeWrite(): void
    {
        // Custom code...

        parent::onBeforeWrite();
    }

    protected function onAfterWrite(): void
    {
        // Custom code...

        parent::onAfterWrite();
    }

    public function requireDefaultRecords(): void
    {
        // Custom code...

        parent::requireDefaultRecords();
    }
}

namespace App\Tests\Model;

final class FooTest extends \SilverStripe\Dev\SapphireTest
{
    protected function setUp(): void
    {
        parent::setUp();

        // Custom code...
    }

    protected function setUpBeforeClass(): void
    {
        parent::setupBeforeClass();

        // Custom code...
    }

    protected function tearDown(): void
    {
        // Custom code...

        parent::tearDown();
    }

    protected function tearDownAfterClass(): void
    {
        // Custom code...

        parent::tearDownAfterClass();
    }
}

👍