Skip to content

Commit

Permalink
Allow using typiphy 0.4 (#23)
Browse files Browse the repository at this point in the history
* Allow using typiphy 0.4

* Add release-2.x to CI
  • Loading branch information
cspray authored Jun 17, 2024
1 parent 5884937 commit ec7204a
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: 'Unit Tests'

on:
push:
branches: [ release-1.x ]
branches: [ release-1.x, release-2.x ]
pull_request:
branches: [ release-1.x ]
branches: [ release-1.x, release-2.x ]

jobs:
build-test:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ use function Cspray\AnnotatedTarget\parseAttributes;
foreach (parseAttributes(__DIR__ . '/src') as $annotatedTarget) {
// $annotatedTarget is an instanceof AnnotatedTarget
// This will be a ReflectionClass, ReflectionProperty, or ReflectionMethod depending on which iteration
$target = $annotatedTarget->getTargetReflection();
$target = $annotatedTarget->targetReflection();
// This will be a ReflectionAttribute
$attributeReflection = $annotatedTarget->getAttributeReflection();
// This will be an instance of the Attribute returned from $this->getAttributeReflection()->newInstance()
$attributeInstance = $annotatedTarget->getAttributeInstance();
$attributeReflection = $annotatedTarget->attributeReflection();
// This will be an instance of the Attribute returned from $this->attributeReflection()->newInstance()
$attributeInstance = $annotatedTarget->attributeInstance();

// All the methods above are shared
$isShared = $annotatedTarget->getTargetReflection() === $annotatedTarget->getTargetReflection(); // true
$isShared = $annotatedTarget->targetReflection() === $annotatedTarget->targetReflection(); // true
}
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": "^8.1",
"nikic/php-parser": "^v4.18 || ^5.0",
"cspray/typiphy": "^0.3"
"cspray/typiphy": "^0.4"
},
"require-dev": {
"pestphp/pest": "^v1.21"
Expand Down
6 changes: 3 additions & 3 deletions src/AnnotatedTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

interface AnnotatedTarget {

public function getTargetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction;
public function targetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction;

public function getAttributeReflection() : ReflectionAttribute;
public function attributeReflection() : ReflectionAttribute;

public function getAttributeInstance() : object;
public function attributeInstance() : object;

}
4 changes: 4 additions & 0 deletions src/AnnotatedTargetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

interface AnnotatedTargetParser {

/**
* @param AnnotatedTargetParserOptions $options
* @return Generator<AnnotatedTarget>
*/
public function parse(AnnotatedTargetParserOptions $options) : Generator;

}
12 changes: 10 additions & 2 deletions src/AnnotatedTargetParserOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace Cspray\AnnotatedTarget;

use Cspray\Typiphy\ObjectType;

interface AnnotatedTargetParserOptions {

public function getSourceDirectories() : array;
/**
* @return list<non-empty-string>
*/
public function sourceDirectories() : array;

public function getAttributeTypes() : array;
/**
* @return list<ObjectType>
*/
public function attributeTypes() : array;

}
4 changes: 2 additions & 2 deletions src/AnnotatedTargetParserOptionsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function build() : AnnotatedTargetParserOptions {

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

public function getSourceDirectories() : array {
public function sourceDirectories() : array {
return $this->directories;
}

public function getAttributeTypes() : array {
public function attributeTypes() : array {
return $this->attributes;
}
};
Expand Down
16 changes: 8 additions & 8 deletions src/PhpParserAnnotatedTargetParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function parse(AnnotatedTargetParserOptions $options) : Generator {
$data->targets = [];
$nodeTraverser->addVisitor($this->getVisitor(
static fn($target) => $data->targets[] = $target,
$options->getAttributeTypes()
$options->attributeTypes()
));

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

private function getSourceIterator(AnnotatedTargetParserOptions $options) : Iterator {
foreach ($options->getSourceDirectories() as $directory) {
foreach ($options->sourceDirectories() as $directory) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS)
);
Expand All @@ -75,7 +75,7 @@ private function getSourceIterator(AnnotatedTargetParserOptions $options) : Iter
}

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

private $consumer;
Expand Down Expand Up @@ -182,23 +182,23 @@ public function __construct(
$this->reflectorSupplier = $reflectorSupplier;
}

public function getTargetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction {
public function targetReflection() : ReflectionClass|ReflectionProperty|ReflectionClassConstant|ReflectionMethod|ReflectionParameter|ReflectionFunction {
if (!isset($this->reflection)) {
$this->reflection = ($this->reflectorSupplier)();
}
return $this->reflection;
}

public function getAttributeReflection() : ReflectionAttribute {
public function attributeReflection() : ReflectionAttribute {
if (!isset($this->reflectionAttribute)) {
$this->reflectionAttribute = $this->getTargetReflection()->getAttributes()[$this->index];
$this->reflectionAttribute = $this->targetReflection()->getAttributes()[$this->index];
}
return $this->reflectionAttribute;
}

public function getAttributeInstance() : object {
public function attributeInstance() : object {
if (!isset($this->attribute)) {
$this->attribute = $this->getAttributeReflection()->newInstance();
$this->attribute = $this->attributeReflection()->newInstance();
}
return $this->attribute;
}
Expand Down
5 changes: 3 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use function Cspray\Typiphy\objectType;

/**
* @param array|string $directories
* @param array $filterAttributes
* @param list<non-empty-string>|non-empty-string $directories
* @param list<class-string> $filterAttributes
* @return Generator<AnnotatedTarget>
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -20,5 +20,6 @@ function parseAttributes(array|string $directories, array $filterAttributes = []
$attributeTypes = array_map(fn($type) => objectType($type), $filterAttributes);
$builder = $builder->filterAttributes(...$attributeTypes);
}

return $parser->parse($builder->build());
}
Loading

0 comments on commit ec7204a

Please sign in to comment.