Skip to content

Commit ab9b0f4

Browse files
authored
Merge pull request #7 from phpDocumentor/feature/composite-specification
Makes composite specification implement Spec
2 parents 96a1c50 + 6ef203d commit ab9b0f4

23 files changed

+119
-155
lines changed

.scrutinizer.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ tools:
4141
filter:
4242
paths: ["src/*", "tests/*"]
4343
sensiolabs_security_checker: true
44+
45+
checks:
46+
php:
47+
excluded_dependencies:
48+
- phpstan/phpstan

src/Finder.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
namespace Flyfinder;
1616

17+
use Flyfinder\Specification\SpecificationInterface;
1718
use Generator;
1819
use League\Flysystem\FilesystemInterface;
1920
use League\Flysystem\PluginInterface;
20-
use Flyfinder\Specification\SpecificationInterface;
2121

2222
/**
2323
* Flysystem plugin to add file finding capabilities to the filesystem entity.
@@ -31,21 +31,16 @@ class Finder implements PluginInterface
3131

3232
/**
3333
* Get the method name.
34-
*
35-
* @return string
3634
*/
37-
public function getMethod() : string
35+
public function getMethod(): string
3836
{
3937
return 'find';
4038
}
4139

4240
/**
4341
* Set the Filesystem object.
44-
*
45-
* @param FilesystemInterface $filesystem
46-
* @return void
4742
*/
48-
public function setFilesystem(FilesystemInterface $filesystem)
43+
public function setFilesystem(FilesystemInterface $filesystem): void
4944
{
5045
$this->filesystem = $filesystem;
5146
}
@@ -55,11 +50,8 @@ public function setFilesystem(FilesystemInterface $filesystem)
5550
*
5651
* Note that only found *files* are yielded at this level,
5752
* which go back to the caller.
58-
*
59-
* @param SpecificationInterface $specification
60-
* @return Generator
6153
*/
62-
public function handle(SpecificationInterface $specification) : Generator
54+
public function handle(SpecificationInterface $specification): Generator
6355
{
6456
foreach ($this->yieldFilesInPath($specification, '') as $path) {
6557
if (isset($path['type']) && $path['type'] === 'file') {
@@ -75,19 +67,16 @@ public function handle(SpecificationInterface $specification) : Generator
7567
* since they have to be recursed into. Yielded directories
7668
* will not make their way back to the caller, as they are filtered out
7769
* by {@link handle()}.
78-
*
79-
* @param SpecificationInterface $specification
80-
* @param string $path
81-
* @return Generator
8270
*/
83-
private function yieldFilesInPath(SpecificationInterface $specification, string $path) : Generator
71+
private function yieldFilesInPath(SpecificationInterface $specification, string $path): Generator
8472
{
8573
$listContents = $this->filesystem->listContents($path);
8674
foreach ($listContents as $location) {
8775
if ($specification->isSatisfiedBy($location)) {
8876
yield $location;
8977
}
90-
if ($location['type'] == 'dir') {
78+
79+
if ($location['type'] === 'dir') {
9180
foreach ($this->yieldFilesInPath($specification, $location['path']) as $returnedLocation) {
9281
yield $returnedLocation;
9382
}

src/Path.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,16 @@ final class Path
2828

2929
/**
3030
* Initializes the path.
31-
*
32-
* @param string $path
3331
*/
3432
public function __construct(string $path)
3533
{
36-
$this->path = (string)$path;
34+
$this->path = $path;
3735
}
3836

3937
/**
4038
* returns a string representation of the path.
41-
*
42-
* @return string
4339
*/
44-
public function __toString() : string
40+
public function __toString(): string
4541
{
4642
return $this->path;
4743
}

src/Specification/AndSpecification.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@
1717
/**
1818
* Class AndSpecification
1919
*/
20-
final class AndSpecification extends CompositeSpecification implements SpecificationInterface
20+
final class AndSpecification extends CompositeSpecification
2121
{
2222
/**
23-
* @var CompositeSpecification
23+
* @var SpecificationInterface
2424
*/
2525
private $one;
2626

2727
/**
28-
* @var CompositeSpecification
28+
* @var SpecificationInterface
2929
*/
3030
private $other;
3131

3232
/**
3333
* Initializes the AndSpecification object
34-
*
35-
* @param CompositeSpecification $one
36-
* @param CompositeSpecification $other
3734
*/
38-
public function __construct(CompositeSpecification $one, CompositeSpecification $other)
35+
public function __construct(SpecificationInterface $one, SpecificationInterface $other)
3936
{
4037
$this->one = $one;
4138
$this->other = $other;
@@ -45,9 +42,8 @@ public function __construct(CompositeSpecification $one, CompositeSpecification
4542
* Checks if the value meets the specification
4643
*
4744
* @param mixed[] $value
48-
* @return bool
4945
*/
50-
public function isSatisfiedBy(array $value) : bool
46+
public function isSatisfiedBy(array $value): bool
5147
{
5248
return $this->one->isSatisfiedBy($value) && $this->other->isSatisfiedBy($value);
5349
}

src/Specification/CompositeSpecification.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,31 @@
1818
* Class CompositeSpecification
1919
* Base class for specifications, allows for combining specifications
2020
*/
21-
abstract class CompositeSpecification
21+
abstract class CompositeSpecification implements SpecificationInterface
2222
{
2323
/**
2424
* Returns a specification that satisfies the original specification
2525
* as well as the other specification
26-
*
27-
* @param CompositeSpecification $other
28-
* @return AndSpecification
2926
*/
30-
public function andSpecification(CompositeSpecification $other) : AndSpecification
27+
public function andSpecification(SpecificationInterface $other): AndSpecification
3128
{
3229
return new AndSpecification($this, $other);
3330
}
3431

3532
/**
3633
* Returns a specification that satisfies the original specification
3734
* or the other specification
38-
*
39-
* @param CompositeSpecification $other
40-
* @return OrSpecification
4135
*/
42-
public function orSpecification(CompositeSpecification $other) : OrSpecification
36+
public function orSpecification(SpecificationInterface $other): OrSpecification
4337
{
4438
return new OrSpecification($this, $other);
4539
}
4640

4741
/**
4842
* Returns a specification that is the inverse of the original specification
4943
* i.e. does not meet the original criteria
50-
*
51-
* @return NotSpecification
5244
*/
53-
public function notSpecification() : NotSpecification
45+
public function notSpecification(): NotSpecification
5446
{
5547
return new NotSpecification($this);
5648
}

src/Specification/HasExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Class HasExtension
1919
* Files and directories meet the specification if they have the given extension
2020
*/
21-
class HasExtension extends CompositeSpecification implements SpecificationInterface
21+
class HasExtension extends CompositeSpecification
2222
{
2323
/**
2424
* @var string[]
@@ -39,9 +39,8 @@ public function __construct(array $extensions)
3939
* Checks if the value meets the specification
4040
*
4141
* @param mixed[] $value
42-
* @return bool
4342
*/
44-
public function isSatisfiedBy(array $value) : bool
43+
public function isSatisfiedBy(array $value): bool
4544
{
4645
return isset($value['extension']) && in_array($value['extension'], $this->extensions) ? true : false;
4746
}

src/Specification/InPath.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Note this behavior is different than in Finder, in that directories *can* meet the spec,
2424
* whereas Finder would never return a directory as "found".
2525
*/
26-
class InPath extends CompositeSpecification implements SpecificationInterface
26+
class InPath extends CompositeSpecification
2727
{
2828
/**
2929
* @var Path
@@ -32,8 +32,6 @@ class InPath extends CompositeSpecification implements SpecificationInterface
3232

3333
/**
3434
* Initializes the InPath specification
35-
*
36-
* @param Path $path
3735
*/
3836
public function __construct(Path $path)
3937
{
@@ -44,9 +42,8 @@ public function __construct(Path $path)
4442
* Checks if the value meets the specification
4543
*
4644
* @param mixed[] $value
47-
* @return bool
4845
*/
49-
public function isSatisfiedBy(array $value) : bool
46+
public function isSatisfiedBy(array $value): bool
5047
{
5148
if (in_array($this->path, ['', '.', './'])) {
5249
/*
@@ -68,8 +65,7 @@ public function isSatisfiedBy(array $value) : bool
6865
if (isset($value['path'])) {
6966
$pattern = '(^(?!\/)'
7067
. str_replace(['?', '*'], [$validChars . '{1}', $validChars . '*'], $path)
71-
. $validChars . '*)'
72-
;
68+
. $validChars . '*)';
7369
if (preg_match($pattern, $value['path'])) {
7470
return true;
7571
}
@@ -81,8 +77,7 @@ public function isSatisfiedBy(array $value) : bool
8177
if (isset($value['dirname'])) {
8278
$pattern = '(^(?!\/)'
8379
. str_replace(['?', '*'], [$validChars . '{1}', $validChars . '*'], $path . '/')
84-
. $validChars . '*)'
85-
;
80+
. $validChars . '*)';
8681
if (preg_match($pattern, $value['dirname'] . '/')) {
8782
return true;
8883
}

src/Specification/IsHidden.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
* Class IsHidden
1919
* Files or directories meet the specification if they are hidden
2020
*/
21-
class IsHidden extends CompositeSpecification implements SpecificationInterface
21+
class IsHidden extends CompositeSpecification
2222
{
2323
/**
2424
* Checks if the value meets the specification
2525
*
2626
* @param mixed[] $value
27-
* @return bool
2827
*/
29-
public function isSatisfiedBy(array $value) : bool
28+
public function isSatisfiedBy(array $value): bool
3029
{
31-
return isset($value['basename']) && substr($value['basename'], 0, 1) === '.' ? true : false;
30+
return isset($value['basename']) && substr($value['basename'], 0, 1) === '.';
3231
}
3332
}

src/Specification/NotSpecification.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
/**
1818
* Class NotSpecification
1919
*/
20-
final class NotSpecification extends CompositeSpecification implements SpecificationInterface
20+
final class NotSpecification extends CompositeSpecification
2121
{
2222
/**
23-
* @var CompositeSpecification
23+
* @var SpecificationInterface
2424
*/
2525
private $wrapped;
2626

2727
/**
2828
* Initializes the NotSpecification object
29-
*
30-
* @param CompositeSpecification $wrapped
3129
*/
32-
public function __construct(CompositeSpecification $wrapped)
30+
public function __construct(SpecificationInterface $wrapped)
3331
{
3432
$this->wrapped = $wrapped;
3533
}
@@ -38,10 +36,9 @@ public function __construct(CompositeSpecification $wrapped)
3836
* Checks if the value meets the specification
3937
*
4038
* @param mixed[] $value
41-
* @return bool
4239
*/
43-
public function isSatisfiedBy(array $value) : bool
40+
public function isSatisfiedBy(array $value): bool
4441
{
45-
return ! $this->wrapped->isSatisfiedBy($value);
42+
return !$this->wrapped->isSatisfiedBy($value);
4643
}
4744
}

src/Specification/OrSpecification.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@
1717
/**
1818
* Class OrSpecification
1919
*/
20-
final class OrSpecification extends CompositeSpecification implements SpecificationInterface
20+
final class OrSpecification extends CompositeSpecification
2121
{
2222
/**
23-
* @var CompositeSpecification
23+
* @var SpecificationInterface
2424
*/
2525
private $one;
2626

2727
/**
28-
* @var CompositeSpecification
28+
* @var SpecificationInterface
2929
*/
3030
private $other;
3131

3232
/**
3333
* Initializes the OrSpecification object
34-
*
35-
* @param CompositeSpecification $one
36-
* @param CompositeSpecification $other
3734
*/
38-
public function __construct(CompositeSpecification $one, CompositeSpecification $other)
35+
public function __construct(SpecificationInterface $one, SpecificationInterface $other)
3936
{
4037
$this->one = $one;
4138
$this->other = $other;
@@ -45,9 +42,8 @@ public function __construct(CompositeSpecification $one, CompositeSpecification
4542
* Checks if the value meets the specification
4643
*
4744
* @param mixed[] $value
48-
* @return bool
4945
*/
50-
public function isSatisfiedBy(array $value) : bool
46+
public function isSatisfiedBy(array $value): bool
5147
{
5248
return $this->one->isSatisfiedBy($value) || $this->other->isSatisfiedBy($value);
5349
}

0 commit comments

Comments
 (0)