Skip to content

Commit

Permalink
Merge pull request #7 from ebln/attributes
Browse files Browse the repository at this point in the history
Add a designated Attribute
  • Loading branch information
ebln committed Jun 7, 2024
2 parents d5c5819 + 1d4e513 commit df3b0fc
Show file tree
Hide file tree
Showing 34 changed files with 1,036 additions and 244 deletions.
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

0 comments on commit df3b0fc

Please sign in to comment.