looglecz/phpstan-magento2-extension

There is no license information available for the latest version (v0.1.8) of this package.

Get best of PHPStan with respect to some Magento2 specialities

Installs: 473

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:phpstan-extension

pkg:composer/looglecz/phpstan-magento2-extension

v0.1.8 2025-03-06 10:58 UTC

This package is auto-updated.

Last update: 2026-01-06 13:01:57 UTC


README

This repository contains set of rule and exclusions for PHPStan in order to be able to get best of the PHPStan static analysis with respect to some nasty things Magento does.

Exclusion of errors

  • Missing type in interceptor method
    • Since you need to copy part of the original method's signature, you are sometimes forced to use not-well typed arguments. This extension will ignore such errors.

Stubs

Various stubs for different classes that are vaguely typed or there is error in the type hint.

Collection templates

Magneto 2 collections offers you a way how to manipulate with objects in bulk. However, PHPStan cannot interfere specific type of item from the collection itself. In this extension, there are stubs for the Collection classes that allows you to have properly typed collection items that can help you with static analysis.

Stubs are available for following classes:

  • \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  • \Magento\Framework\Data\Collection\AbstractDb
  • \Magento\Framework\Data\Collection

Usage

All you need to do is include @extends AbstractCollection<\Vendor\Module\Model\Element> in class-level PHPDoc. For example:

<?php

declare(strict_types=1);

namespace Vendor\Module\Model\ResourceModel\Element;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

/**
 * @extends AbstractCollection<\Vendor\Module\Model\Element>
 */
class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            \Vendor\Module\Model\Element::class,
            \Vendor\Module\Model\ResourceModel\Element::class
        );
    }
}

If you do not work with DB collection, you will use similar annotation for other collection classes.