Skip to content

Commit ec7204a

Browse files
authored
Allow using typiphy 0.4 (#23)
* Allow using typiphy 0.4 * Add release-2.x to CI
1 parent 5884937 commit ec7204a

11 files changed

+86
-73
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: 'Unit Tests'
22

33
on:
44
push:
5-
branches: [ release-1.x ]
5+
branches: [ release-1.x, release-2.x ]
66
pull_request:
7-
branches: [ release-1.x ]
7+
branches: [ release-1.x, release-2.x ]
88

99
jobs:
1010
build-test:

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ use function Cspray\AnnotatedTarget\parseAttributes;
5050
foreach (parseAttributes(__DIR__ . '/src') as $annotatedTarget) {
5151
// $annotatedTarget is an instanceof AnnotatedTarget
5252
// This will be a ReflectionClass, ReflectionProperty, or ReflectionMethod depending on which iteration
53-
$target = $annotatedTarget->getTargetReflection();
53+
$target = $annotatedTarget->targetReflection();
5454
// This will be a ReflectionAttribute
55-
$attributeReflection = $annotatedTarget->getAttributeReflection();
56-
// This will be an instance of the Attribute returned from $this->getAttributeReflection()->newInstance()
57-
$attributeInstance = $annotatedTarget->getAttributeInstance();
55+
$attributeReflection = $annotatedTarget->attributeReflection();
56+
// This will be an instance of the Attribute returned from $this->attributeReflection()->newInstance()
57+
$attributeInstance = $annotatedTarget->attributeInstance();
5858

5959
// All the methods above are shared
60-
$isShared = $annotatedTarget->getTargetReflection() === $annotatedTarget->getTargetReflection(); // true
60+
$isShared = $annotatedTarget->targetReflection() === $annotatedTarget->targetReflection(); // true
6161
}
6262
```
6363

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"require": {
1414
"php": "^8.1",
1515
"nikic/php-parser": "^v4.18 || ^5.0",
16-
"cspray/typiphy": "^0.3"
16+
"cspray/typiphy": "^0.4"
1717
},
1818
"require-dev": {
1919
"pestphp/pest": "^v1.21"

src/AnnotatedTarget.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
interface AnnotatedTarget {
1414

15-
public function getTargetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction;
15+
public function targetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction;
1616

17-
public function getAttributeReflection() : ReflectionAttribute;
17+
public function attributeReflection() : ReflectionAttribute;
1818

19-
public function getAttributeInstance() : object;
19+
public function attributeInstance() : object;
2020

2121
}

src/AnnotatedTargetParser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
interface AnnotatedTargetParser {
88

9+
/**
10+
* @param AnnotatedTargetParserOptions $options
11+
* @return Generator<AnnotatedTarget>
12+
*/
913
public function parse(AnnotatedTargetParserOptions $options) : Generator;
1014

1115
}

src/AnnotatedTargetParserOptions.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
namespace Cspray\AnnotatedTarget;
44

5+
use Cspray\Typiphy\ObjectType;
6+
57
interface AnnotatedTargetParserOptions {
68

7-
public function getSourceDirectories() : array;
9+
/**
10+
* @return list<non-empty-string>
11+
*/
12+
public function sourceDirectories() : array;
813

9-
public function getAttributeTypes() : array;
14+
/**
15+
* @return list<ObjectType>
16+
*/
17+
public function attributeTypes() : array;
1018

1119
}

src/AnnotatedTargetParserOptionsBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function build() : AnnotatedTargetParserOptions {
4040

4141
public function __construct(private readonly array $directories, private readonly array $attributes) {}
4242

43-
public function getSourceDirectories() : array {
43+
public function sourceDirectories() : array {
4444
return $this->directories;
4545
}
4646

47-
public function getAttributeTypes() : array {
47+
public function attributeTypes() : array {
4848
return $this->attributes;
4949
}
5050
};

src/PhpParserAnnotatedTargetParser.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function parse(AnnotatedTargetParserOptions $options) : Generator {
4040
$data->targets = [];
4141
$nodeTraverser->addVisitor($this->getVisitor(
4242
static fn($target) => $data->targets[] = $target,
43-
$options->getAttributeTypes()
43+
$options->attributeTypes()
4444
));
4545

4646
foreach ($this->getSourceIterator($options) as $sourceFile) {
@@ -61,7 +61,7 @@ public function parse(AnnotatedTargetParserOptions $options) : Generator {
6161
}
6262

6363
private function getSourceIterator(AnnotatedTargetParserOptions $options) : Iterator {
64-
foreach ($options->getSourceDirectories() as $directory) {
64+
foreach ($options->sourceDirectories() as $directory) {
6565
$iterator = new RecursiveIteratorIterator(
6666
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
6767
);
@@ -75,7 +75,7 @@ private function getSourceIterator(AnnotatedTargetParserOptions $options) : Iter
7575
}
7676

7777
private function getVisitor(callable $consumer, array $filteredAttributes) : NodeVisitor {
78-
$filteredAttributes = array_map(fn($attr) => $attr->getName(), $filteredAttributes);
78+
$filteredAttributes = array_map(fn($attr) => $attr->name(), $filteredAttributes);
7979
return new class($consumer, $filteredAttributes) extends NodeVisitorAbstract {
8080

8181
private $consumer;
@@ -182,23 +182,23 @@ public function __construct(
182182
$this->reflectorSupplier = $reflectorSupplier;
183183
}
184184

185-
public function getTargetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction {
185+
public function targetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction {
186186
if (!isset($this->reflection)) {
187187
$this->reflection = ($this->reflectorSupplier)();
188188
}
189189
return $this->reflection;
190190
}
191191

192-
public function getAttributeReflection() : ReflectionAttribute {
192+
public function attributeReflection() : ReflectionAttribute {
193193
if (!isset($this->reflectionAttribute)) {
194-
$this->reflectionAttribute = $this->getTargetReflection()->getAttributes()[$this->index];
194+
$this->reflectionAttribute = $this->targetReflection()->getAttributes()[$this->index];
195195
}
196196
return $this->reflectionAttribute;
197197
}
198198

199-
public function getAttributeInstance() : object {
199+
public function attributeInstance() : object {
200200
if (!isset($this->attribute)) {
201-
$this->attribute = $this->getAttributeReflection()->newInstance();
201+
$this->attribute = $this->attributeReflection()->newInstance();
202202
}
203203
return $this->attribute;
204204
}

src/functions.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use function Cspray\Typiphy\objectType;
77

88
/**
9-
* @param array|string $directories
10-
* @param array $filterAttributes
9+
* @param list<non-empty-string>|non-empty-string $directories
10+
* @param list<class-string> $filterAttributes
1111
* @return Generator<AnnotatedTarget>
1212
* @throws Exception\InvalidArgumentException
1313
*/
@@ -20,5 +20,6 @@ function parseAttributes(array|string $directories, array $filterAttributes = []
2020
$attributeTypes = array_map(fn($type) => objectType($type), $filterAttributes);
2121
$builder = $builder->filterAttributes(...$attributeTypes);
2222
}
23+
2324
return $parser->parse($builder->build());
2425
}

0 commit comments

Comments
 (0)