Skip to content

Commit df3b0fc

Browse files
authored
Merge pull request #7 from ebln/attributes
Add a designated Attribute
2 parents d5c5819 + 1d4e513 commit df3b0fc

34 files changed

+1036
-244
lines changed

.github/workflows/buildTest.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
name: Build and test on ${{ matrix.php }}
1313
steps:
1414
- name: Checkout repository
15-
uses: actions/checkout@v2
15+
uses: actions/checkout@v4
1616
- name: Force PHP ${{ matrix.php }}
17-
uses: nanasess/setup-php@master
17+
uses: nanasess/setup-php@v4
1818
with:
1919
php-version: ${{ matrix.php }}
2020
- name: Validate composer.json and composer.lock
@@ -37,11 +37,10 @@ jobs:
3737
./vendor/bin/phpstan --no-interaction --no-ansi analyse
3838
- name: Mess Detector Sources
3939
run: |
40-
./vendor/bin/phpmd src text codesize,controversial,design,naming,unusedcode,design
41-
- name: Mess Detector Tests
42-
run: |
43-
./vendor/bin/phpmd tests text codesize,controversial,design
44-
./vendor/bin/phpmd src text codesize,controversial,naming,unusedcode
40+
./vendor/bin/phpmd src text codesize,controversial,naming,unusedcode
41+
- name: Mess Detector Tests
42+
run: |
43+
./vendor/bin/phpmd tests text codesize,controversial,design
4544
- name: php-cs-fixer
4645
run: |
4746
./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix -v --config=.php-cs-fixer.dist.php --using-cache=no --dry-run

CHANGELOG.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)
55

66
----
77

8-
## Upcoming
8+
## Upcomming
9+
10+
- Remove support for the interface
11+
- Deprecate (abandon) the interface package
12+
- create conflict with interface for version 2
13+
```json
14+
{
15+
"conflict": {
16+
"ebln/ebln/phpstan-factory-mark": "*"
17+
}
18+
}
19+
```
920

21+
----
22+
23+
## [1.0.0]
24+
25+
### Added
1026
- Support for attributes
1127

12-
----
28+
### Removed
29+
* Support for PHP < 7.4
30+
* Support for PHPStan < 1.11
1331

1432
## [0.0.2]
1533

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,44 @@ ebln/phpstan-factory-rule
33

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

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

8-
Install this package and the marking package alongside with PHPStan.
9+
Add the `ForceFactory` attribute to your class, and supply all class names as arguments,
10+
which shall be allowed to instanciate your object.
11+
```php
12+
<?php
13+
// […]
14+
15+
#[\Ebln\Attrib\ForceFactory(GoodFactory::class, BetterFactory::class)]
16+
class OnlyViaFactory
17+
{
18+
}
19+
```
20+
21+
Now lean back and rely on PHPStan in CI pipelines, git hooks or IDE integrations;
22+
If somebody introduces a rogue factory:
23+
```php
24+
<?php
25+
// […]
26+
27+
class FailingFactory
28+
{
29+
public function create(): OnlyViaFactory
30+
{
31+
return new OnlyViaFactory();
32+
}
33+
}
34+
```
35+
…that is supposed to fail, when you run PHPStan.
36+
37+
## Deprecated usage with `ebln/phpstan-factory-mark`
38+
39+
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:
40+
```shell
41+
composer require ebln/phpstan-factory-mark
42+
composer require --dev ebln/phpstan-factory-rule
43+
```
944

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

4378
## Installation
4479

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

47-
```
48-
composer require ebln/phpstan-factory-mark && composer require --dev ebln/phpstan-factory-rule
82+
```php
83+
composer require --dev ebln/phpstan-factory-rule
4984
```
5085

5186
If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!

attrib/ForceFactory.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ebln\Attrib;
6+
7+
/**
8+
* Marks classes to be instanciated by certain factories
9+
*
10+
* If used together with ForceFactoryInterface
11+
* the configured factories must be congruent!
12+
* This is enforced for PHP 8 and later.
13+
*/
14+
#[\Attribute(\Attribute::TARGET_CLASS)]
15+
class ForceFactory
16+
{
17+
/** @var array<int, class-string> */
18+
private array $allowedFactories;
19+
20+
/** @param class-string ...$factories */
21+
public function __construct(string ...$factories)
22+
{
23+
$allowedFactories = [];
24+
foreach ($factories as $factory) {
25+
$allowedFactories[$factory] = $factory;
26+
}
27+
28+
$this->allowedFactories = array_values($allowedFactories);
29+
}
30+
31+
/** @return array<int, class-string> */
32+
public function getAllowedFactories(): array
33+
{
34+
return $this->allowedFactories;
35+
}
36+
}

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
"phpmd/phpmd": "^2.10",
2020
"phpunit/phpunit": "^9.5",
2121
"roave/security-advisories": "dev-latest",
22-
"vimeo/psalm": "^4.12"
22+
"vimeo/psalm": "^5.24"
2323
},
2424
"autoload": {
2525
"psr-4": {
26+
"Ebln\\Attrib\\": "attrib/",
2627
"Ebln\\PHPStan\\EnforceFactory\\": "src/"
2728
}
2829
},
@@ -52,7 +53,7 @@
5253
"psalm --find-unused-psalm-suppress",
5354
"phpstan analyse",
5455
"@style-check",
55-
"phpmd src ansi codesize,controversial,design,naming,unusedcode,design",
56+
"phpmd src ansi codesize,controversial,naming,unusedcode",
5657
"phpmd tests ansi codesize,controversial,design"
5758
],
5859
"style-check": "php-cs-fixer fix -v --config=.php-cs-fixer.dist.php --using-cache=no --dry-run",

0 commit comments

Comments
 (0)