Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit 09c0213

Browse files
committed
Updated README and applies PHPStan fixes
1 parent 448ffd0 commit 09c0213

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ Path Finder
33

44
[![Build Status](https://travis-ci.org/phpactor/path-finder.svg?branch=master)](https://travis-ci.org/phpactor/path-finder)
55

6-
Library to infer paths from a given path where paths share the same "kernel"
7-
(common section of the path).
6+
Library to infer paths from a given path where paths share path segments.
87

9-
For example, infer unit test path from the source file, from a unit test to
10-
a bechmark, from the benchmark to the source file etc.
8+
For example, infer test paths for a given source file and vice-versa.
119

1210
Usage
1311
-----
1412

1513
Path finder accepts a hash map of destinations and their schemas. The
16-
"kernel" is a place holder for the common path segment that all destinations
17-
share:
14+
placeholders can be used to identify common parts of the path.
15+
16+
- The last placeholder is _greedy_ it will match all path segments until the
17+
suffix.
18+
- Preceding placeholders will only match until the first path separator.
19+
20+
Examples
21+
--------
22+
23+
### Navigating between test files
1824

1925
```php
2026
$pathFinder = PathFinder::fromDestinations([
@@ -25,9 +31,25 @@ $pathFinder = PathFinder::fromDestinations([
2531

2632
$targets = $pathFinder->targetsFor('lib/Acme/Post.php');
2733

28-
var_dump($targets);
2934
// [
3035
// 'unit_test' => 'tests/Unit/Acme/PostTest.php',
3136
// 'benchmark' => 'benchmarks/Acme/PostBench.php',
3237
// ]
3338
```
39+
40+
### Navigating between files organized by domain/module
41+
42+
```php
43+
$pathFinder = PathFinder::fromDestinations([
44+
'source' => 'lib/<module>/<kernel>.php',
45+
'unit_test' => 'tests/<module>Unit/<kernel>Test.php',
46+
'benchmark' => 'benchmarks/<module>/<kernel>Bench.php',
47+
]);
48+
49+
$targets = $pathFinder->targetsFor('lib/MyModule/Acme/Post.php');
50+
51+
// [
52+
// 'unit_test' => 'tests/MyModule/Unit/Acme/PostTest.php',
53+
// 'benchmark' => 'benchmarks/MyMOdule/Acme/PostBench.php',
54+
// ]
55+
```

lib/PathFinder.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,30 @@ class PathFinder
1212
*/
1313
private $destinations = [];
1414

15+
/**
16+
* @param array<string, Pattern> $destinations
17+
*/
1518
private function __construct(array $destinations)
1619
{
17-
foreach ($destinations as $destinationName => $pattern) {
18-
$this->add($destinationName, $pattern);
19-
}
20+
$this->destinations = $destinations;
2021
}
2122

23+
/**
24+
* @param array<string, string> $destinations
25+
*/
2226
public static function fromDestinations(array $destinations): PathFinder
2327
{
24-
return new self($destinations);
28+
return new self(array_map(function (string $pattern) {
29+
return Pattern::fromPattern($pattern);
30+
}, $destinations));
2531
}
2632

2733
/**
2834
* Return a hash map of destination names to paths representing
2935
* paths which relate to the given file path.
3036
*
3137
* @throws NoMatchingSourceException
38+
* @return array<string,string>
3239
*/
3340
public function destinationsFor(string $filePath): array
3441
{
@@ -48,11 +55,6 @@ public function destinationsFor(string $filePath): array
4855
return $destinations;
4956
}
5057

51-
private function add($destinationName, $pattern): void
52-
{
53-
$this->destinations[$destinationName] = Pattern::fromPattern($pattern);
54-
}
55-
5658
private function findSourcePattern(string $filePath): Pattern
5759
{
5860
foreach ($this->destinations as $name => $pattern) {

lib/Pattern.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Pattern
1616
private $regex;
1717

1818
/**
19-
* @var array
19+
* @var array<string>
2020
*/
2121
private $tokenNames;
2222

@@ -25,6 +25,9 @@ class Pattern
2525
*/
2626
private $pattern;
2727

28+
/**
29+
* @param array<string> $tokenNames
30+
*/
2831
public function __construct(string $regex, string $pattern, array $tokenNames)
2932
{
3033
$this->regex = $regex;
@@ -59,16 +62,30 @@ public function fits(string $filePath): bool
5962
return (bool)preg_match($this->regex, Path::canonicalize($filePath));
6063
}
6164

65+
/**
66+
* @return array<string, string>
67+
*/
6268
public function tokens(string $filePath): array
6369
{
6470
$filePath = Path::canonicalize($filePath);
65-
preg_match($this->regex, $filePath, $matches);
66-
return array_intersect_key($matches, array_combine($this->tokenNames, $this->tokenNames));
71+
72+
if (!preg_match($this->regex, $filePath, $matches)) {
73+
throw new RuntimeException(sprintf(
74+
'Error occurred performing regex on filepath "%s" with regex "%s"',
75+
$filePath,
76+
$this->regex
77+
));
78+
}
79+
80+
return array_intersect_key($matches, (array)array_combine($this->tokenNames, $this->tokenNames));
6781
}
6882

83+
/**
84+
* @param array<string,string> $tokens
85+
*/
6986
public function replaceTokens(array $tokens): string
7087
{
71-
return strtr($this->pattern, array_combine(array_map(function (string $key) {
88+
return strtr($this->pattern, (array)array_combine(array_map(function (string $key) {
7289
return '<' . $key . '>';
7390
}, array_keys($tokens)), array_values($tokens)));
7491
}

phpstan.neon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
includes:
2-
- vendor/phpstan/phpstan/conf/config.level7.neon
3-
41
parameters:
2+
level: 7
53
inferPrivatePropertyTypeFromConstructor: true
64
ignoreErrors:
75

tests/Unit/PathFinderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Phpactor\ClassFileConverter\Exception\NoPlaceHoldersException;
77
use Phpactor\ClassFileConverter\PathFinder;
88
use Phpactor\ClassFileConverter\Exception\NoMatchingSourceException;
9-
use RuntimeException;
109

1110
class PathFinderTest extends TestCase
1211
{

0 commit comments

Comments
 (0)