GithubHelp home page GithubHelp logo

phpstan-rules's Introduction

PHPStan rules

@overrides or must call parent

If there is overriding method, it must either has annotation @overrides or must call parent method.

class BasePresenter {

  private $dep;

  public function injectFoo(Dependency $dep): void {
      $this->dep = $dep
  }

}

class LoginPresenter extends BasePresenter {

  private $dep;
  
  public function injectFoo(Dependency $dep): void {
      $this->dep = $dep
  }

}

Requires LoginPresenter to be explicitly tell that you are intentionally overriding method:

class LoginPresenter extends BasePresenter {

  private $dep;
  
  /**
   * @overrides
   */
  public function injectFoo(Dependency $dep): void {
      $this->dep = $dep
  }

}

or call parent function:

class LoginPresenter extends BasePresenter {

  private $dep;
  
  public function injectFoo(Dependency $dep): void {
      $this->dep = $dep
      parent::injectFoo($dep);
  }

}

Sealed classes

Sealed classes are used for representing restricted class hierarchies.

// file: enum.php

/**
 * @sealed
 */
abstract class EnumFoo {
}

final class Value1 extends EnumFoo {}
final class Value2 extends EnumFoo {}
final class Value3 extends EnumFoo {}

// file: other.php

final class Value4 extends EnumFoo {} // not allowed

This restricts declaring any other sub-type of EnumFoo, then Value1, Value2, Value3. This is usefull, because it should then understand that:

\assert($value instanceof EnumFoo);

switch (getclass($value)): {
  case Value1::class:
    break;
  case Value2::class:
    break;
  case Value3::class:
    break;
  default:
    echo "unreachable code!";
    break;
}

Type system should know that default case is unreachable, because there cannot be any other type decalared anywhere.

Overriding property in child class

class BasePresenter {

  /** @var Dependency */
  public $dep;

}

class LoginPresenter extends BasePresenter {

  /** @var Dependency2 */
  public $dep; // error

}

require_once return value used

This is usually not what you expect.

phpstan-rules's People

Contributors

jkuchar avatar

Watchers

James Cloos avatar  avatar

phpstan-rules's Issues

Overrides file missing

Hi, I just stumbled upon this repo and I really like idea of overrides rule. But it seems it is missing in source code. Is it just unfinished idea or only uncommited file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.