Giter Club home page Giter Club logo

phpstan-cakephp2's Introduction

PHPStan-CakePHP2

PHPStan extensions to help test CakePHP 2 projects with PHPStan

Requirements

  • PHP 7.4
  • phpstan/phpstan 1.9+
  • cakephp/cakephp 2.x

Installation

Installation is best done through composer:

composer require --dev ariddlestone/phpstan-cakephp2

You will need to make sure the extension is included in your phpstan config:

# phpstan.neon
includes:
  - vendor/ariddlestone/phpstan-cakephp2/extension.neon

If you have behavior classes in odd locations (perhaps in a vendor directory) you will need to add those locations to your configuration. For example:

# phpstan.neon
parameters:
  ModelBehaviorsExtension:
    behaviorPaths:
      - vendor/my-vendor/my-plugin/src/Model/Behavior/*.php

See extension.neon for the default list of behavior locations.

Features

The following features are added to PHPStan:

  • Treat behavior methods as extra methods on all models ($model->behaviorMethod())
  • Treat controller properties named after model classes as instances of those classes ($controller->Model)
  • Treat controller properties named after component classes as instances of those classes ($controller->Component)
  • Treat component properties names after component classes as instances of those classes ($component->Component)
  • Treat ClassRegistry::init($className) as returning an instance of $className where possible

phpstan-cakephp2's People

Contributors

ariddlestone avatar sidz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

sidz

phpstan-cakephp2's Issues

Add Model Models support

Models can have other models as properties, through relationships.

The properties are named after the relationship name, which is not necessarily the same as the model name.

Add extension for schema files

  • Correctly type $event in before() and after()
  • Correctly type public properties which are added in the schema

Apply to all files which extend CakeSchema.

PHP versions too generous

composer.json says that PHP versions 7.2-8.x should work, but there are issues running it on versions other than 7.4:

  • In 7.2 and 7.3 composer install fails, with required libraries not being compatible with PHP < 7.4
  • In 7.2 and 7.3, even with compatible libraries, composer phpstan fails because a ReflectionEnum class we use is not compatible with PHP < 7.4 (due to phpstan bundling its dependencies, but not updating its requirements to match)
  • In 8.0 we can't install cakephp/cakephp 2.x

Add stub/extension for models

Add stubs and/or extensions to handle things like

  • find('count') returning int|false (not array)
  • findByX
  • findAllByX

Undefined method appears when Component and Model have the same name

Hi.

Thanks for this brilliant extension first of all.

I'm working with project where all Models don't have Model suffix. Like described in the official documentation (https://book.cakephp.org/2/en/models.html#understanding-models)

So in case we have Component and Model with the same name it leads to Call to an undefined method BasicComponent::some_method.

Code sample:

<?php

class BasicComponent extends Component {}
<?php

class Basic extends Model
{
    public function some_method()
    {
    }
}
<?php

class FakeController extends Controller
{
    public $uses = [
        'Basic',
    ];

    public function add_smth()
    {
        $this->Basic->some_method();
    }
}

As I can see component service is setup first so probably it found BasicComponent and tries to look method inside of it only and doesn't take into account that models is also exists.

Add details to readme

We should add the following details to the README.md file:

  • Requirements
  • Installation
  • Features
    • Model behaviors
    • Controller models
    • Controller components
    • Component components
    • ClassRegistry

Restore PHP8+ support

Some people are trying to get CakePHP 2 running on PHP8...

They're mad, but we'll let them use this package to help.

If we allow PHP8, the require-dev list will still prevent development of this package, as the cakephp/cakephp ^2.10.24 requirement won't allow it.

Fix controller component names

Controller component properties do not include the "Component" suffix. The test should be changed as follows:

From:

/** @var BasicController $controller */
$component = $controller->BasicComponent;

assertType('BasicComponent', $component);

to:

/** @var BasicController $controller */
$component = $controller->Basic;

assertType('BasicComponent', $component);

Add extension for FlashComponent

FlashComponent::_call($name, $args) renders the message using app/View/Element/Flash/$name.ctp.

For example, $controller->Flash->success('My message') would render the message using app/View/Element/success.ctp.

We should check for the template's existence, and then provide the correct types etc.

Add component components support

Components can themselves have components using the $components property. We should add a property reflection extension to support this.

Look in AppSchema for tables

When checking whether a model exists (determining the types of controller properties, for example), check app/Config/schema.php if it exists, to get tables which don't have models. If it does, return a generic Model type.

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.