Enforce that your classes get only instantiated by the factories you define!

Overview

ebln/phpstan-factory-rule

Enforce that your classes get only instantiated by the factories you define!

Usage

Install this package and the marking package alongside with PHPStan.

Implement \Ebln\PHPStan\EnforceFactory\ForceFactoryInterface with the DTO you want to protect.


// […]
use Ebln\PHPStan\EnforceFactory\ForceFactoryInterface;

class OnlyViaFactory implements ForceFactoryInterface
{
    // […]
    public static function getFactories(): array
    {   // Return a list of classes that are allowed to
        //   create new OnlyViaFactory instances…
        return [TheOnlyTrueFactory::class];
    }
}

Now rely on PHPStan in CI pipelines, git hooks or IDE integrations.

If somebody introduces a rogue factory:


// […]

class FailingFactory
{
    public function create(): OnlyViaFactory
    {   
        return new OnlyViaFactory();
    }
}

…that is supposed to fail, when you run PHPStan.

Installation

Require this extention and the package containing the interface via Composer:

composer require ebln/phpstan-factory-mark && composer require --dev ebln/phpstan-factory-rule

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, just specify the rule in your project's PHPStan config:

rules:
    - \Ebln\PHPStan\EnforceFactory\ForceFactoryRule
You might also like...
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule for validation Japanese string only full-width or only half-width.

Japanese String Helpers PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule f

WeExpire is an opensource tool for creating emergency notes that can be read by your trusted contacts only after your death or if you are seriously injured

WeExpire is an opensource tool for creating emergency notes that can be read by your trusted contacts only after your death or if you are

A set of PHP classes to let you manage your Dynamic Widget UI programmatically.

Dynamic Widget for Flutter A set of PHP classes to let you manage your Dynamic Widget UI programmatically. Why? Why not? Developing my application I f

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values. Miniset

Secure the data of your sites by encrypting them. They will be decrypted only in your applications

PHP Crypter Secure the data of your sites by encrypting them. They will be decrypted only in your applications How to use ? You just have to include t

A super simple, clean and pretty error handler that replace the default error handler of PHP. You need only include this file!

php-custom-error-handler A super simple, clean and pretty error handler that replace the default error handler of PHP. You need just include only this

A customizable decentralized micro-blog (Twitter) and cms (Wordpress) that only you host and moderate.

Consider this -- each Miter account is individually hosted on a user's personal and private third party server (host) - either purchased or free - as

WooCommerce Empty Cart Button plugin helps you to add Empty Cart button to WooCommerce Pages/Sections using Shortcode only.

WooCommerce Empty Cart Button plugin helps you to add Empty Cart button to WooCommerce Pages/Sections using Shortcode only. How to use? Download the p

Comments
  • ¡WIP! Prototype for Attribute support

    ¡WIP! Prototype for Attribute support

    Attribute Support Spike

    TODO

    • PHPStan inspection of the very Rule crashes
      • similar as https://github.com/phpstan/phpstan/issues/5954
      • reproducable by running PHPStan within the container
    • \Ebln\PHPStan\EnforceFactory\ForceFactory needs to be outsourced to ebln/phpstan-factory-mark ^2
    • Rule itself works, backed by vendor/bin/phpunit
    opened by ebln 0
Releases(0.0.1)
Owner
null
A Laravel 9 package that allows you enforce security of your artisan commands by authenticating users before running.

Introduction This package allows you as a developer to restrict who can and cannot run artisan commands, especially in a production environment. For e

YOo Slim 2 Sep 15, 2022
Test and enforce architectural rules in your Laravel applications. Keep your app's architecture clean and consistent!

Laravel Arkitect Laravel Arkitect lets you test and enforce your architectural rules in your Laravel applications, and it's a PHPArkitect wrapper for

SMorteza Ebadi 55 Dec 17, 2022
Task for GrumPHP that adds CSS linting support with stylelint. An easy way to enforce convention and avoid errors in your styles

grumphp-stylelint-task Installation Stylelint is a static analysis tool for styles. A mighty, modern linter that helps you avoid errors and enforce co

null 3 Apr 29, 2021
A Pocketmine-MP (PMMP) plugin to help staff members enforce the rules of the server.

StaffMode is an all-in-one Pocketmine-MP (PMMP) moderation plugin made to simplify the life of staff members.

ItsMax123 9 Sep 17, 2022
The package provides an expressive "fluent" way to define model attributes.

The package provides an expressive "fluent" way to define model attributes. It automatically builds casts at the runtime and adds a native autocompletion to the models' properties.

Boris Lepikhin 506 Dec 28, 2022
Allow composer packages to define compilation steps

Composer Compile Plugin The "Compile" plugin enables developers of PHP libraries to define free-form "compilation" tasks, such as: Converting SCSS to

CiviCRM 11 Aug 3, 2022
This package provides a set of factories to be used with containers using the PSR-11 standard for an easy Doctrine integration in a project

psr-container-doctrine: Doctrine Factories for PSR-11 Containers Doctrine factories for PSR-11 containers. This package provides a set of factories to

Roave, LLC 84 Dec 14, 2022
Attributes to define PHP language extensions (to be enforced by static analysis)

PHP Language Extensions (currently in BETA) This library provides attributes for extending the PHP language (e.g. adding package visibility). The inte

Dave Liddament 70 Dec 19, 2022
A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and Tailwind CSS

Navite A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and T

Celionatti 2 Aug 22, 2022
uses laravel breeze , CRUD, soft delete, Factories

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Roman Hossain Shaon 2 Sep 27, 2022