Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a designated Attribute #7

Merged
merged 13 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/buildTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
name: Build and test on ${{ matrix.php }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Force PHP ${{ matrix.php }}
uses: nanasess/setup-php@master
uses: nanasess/setup-php@v4
with:
php-version: ${{ matrix.php }}
- name: Validate composer.json and composer.lock
Expand All @@ -37,11 +37,10 @@ jobs:
./vendor/bin/phpstan --no-interaction --no-ansi analyse
- name: Mess Detector Sources
run: |
./vendor/bin/phpmd src text codesize,controversial,design,naming,unusedcode,design
- name: Mess Detector Tests
run: |
./vendor/bin/phpmd tests text codesize,controversial,design
./vendor/bin/phpmd src text codesize,controversial,naming,unusedcode
./vendor/bin/phpmd src text codesize,controversial,naming,unusedcode
- name: Mess Detector Tests
run: |
./vendor/bin/phpmd tests text codesize,controversial,design
- name: php-cs-fixer
run: |
./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix -v --config=.php-cs-fixer.dist.php --using-cache=no --dry-run
22 changes: 20 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)

----

## Upcoming
## Upcomming

- Remove support for the interface
- Deprecate (abandon) the interface package
- create conflict with interface for version 2
```json
{
"conflict": {
"ebln/ebln/phpstan-factory-mark": "*"
}
}
```

----

## [1.0.0]

### Added
- Support for attributes

----
### Removed
* Support for PHP < 7.4
* Support for PHPStan < 1.11

## [0.0.2]

Expand Down
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,44 @@ ebln/phpstan-factory-rule

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

## Usage
## Usage with support for attributes
Require this package: `composer require --dev ebln/phpstan-factory-rule`

Install this package and the marking package alongside with PHPStan.
Add the `ForceFactory` attribute to your class, and supply all class names as arguments,
which shall be allowed to instanciate your object.
```php
<?php
// […]

#[\Ebln\Attrib\ForceFactory(GoodFactory::class, BetterFactory::class)]
class OnlyViaFactory
{
}
```

Now lean back and rely on PHPStan in CI pipelines, git hooks or IDE integrations;
If somebody introduces a rogue factory:
```php
<?php
// […]

class FailingFactory
{
public function create(): OnlyViaFactory
{
return new OnlyViaFactory();
}
}
```
…that is supposed to fail, when you run PHPStan.

## Deprecated usage with `ebln/phpstan-factory-mark`

Require this extention and [the package containing the marking interface](https://github.com/ebln/phpstan-factory-mark) via [Composer](https://getcomposer.org/) alongside with PHPStan:
```shell
composer require ebln/phpstan-factory-mark
composer require --dev ebln/phpstan-factory-rule
```

Implement `\Ebln\PHPStan\EnforceFactory\ForceFactoryInterface` with the DTO you want to protect.
```php
Expand Down Expand Up @@ -42,10 +77,10 @@ class FailingFactory

## Installation

Require this extention and [the package containing the interface](https://github.com/ebln/phpstan-factory-mark) via [Composer](https://getcomposer.org/):
Require this extention via [Composer](https://getcomposer.org/):

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

If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!
Expand Down
36 changes: 36 additions & 0 deletions attrib/ForceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Ebln\Attrib;

/**
* Marks classes to be instanciated by certain factories
*
* If used together with ForceFactoryInterface
* the configured factories must be congruent!
* This is enforced for PHP 8 and later.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class ForceFactory
{
/** @var array<int, class-string> */
private array $allowedFactories;

/** @param class-string ...$factories */
public function __construct(string ...$factories)
{
$allowedFactories = [];
foreach ($factories as $factory) {
$allowedFactories[$factory] = $factory;
}

$this->allowedFactories = array_values($allowedFactories);
}

/** @return array<int, class-string> */
public function getAllowedFactories(): array
{
return $this->allowedFactories;
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"phpmd/phpmd": "^2.10",
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-latest",
"vimeo/psalm": "^4.12"
"vimeo/psalm": "^5.24"
},
"autoload": {
"psr-4": {
"Ebln\\Attrib\\": "attrib/",
"Ebln\\PHPStan\\EnforceFactory\\": "src/"
}
},
Expand Down Expand Up @@ -52,7 +53,7 @@
"psalm --find-unused-psalm-suppress",
"phpstan analyse",
"@style-check",
"phpmd src ansi codesize,controversial,design,naming,unusedcode,design",
"phpmd src ansi codesize,controversial,naming,unusedcode",
"phpmd tests ansi codesize,controversial,design"
],
"style-check": "php-cs-fixer fix -v --config=.php-cs-fixer.dist.php --using-cache=no --dry-run",
Expand Down
Loading