Skip to content

Commit 9867caa

Browse files
committed
add file property to SearchResult class
1 parent 144e860 commit 9867caa

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/Results/FileSearchResults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function addLocation(CodeLocation $location): self
3030
{
3131
$snippet = $this->makeSnippet($location->startLine());
3232

33-
$this->results[] = new SearchResult($location, $snippet);
33+
$this->results[] = new SearchResult($location, $snippet, $this->file);
3434

3535
return $this;
3636
}

src/Results/SearchResult.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Permafrost\PhpCodeSearch\Code\CodeLocation;
66
use Permafrost\PhpCodeSearch\Code\CodeSnippet;
77
use Permafrost\PhpCodeSearch\Code\FunctionCallLocation;
8+
use Permafrost\PhpCodeSearch\Support\File;
89

910
class SearchResult
1011
{
@@ -14,9 +15,23 @@ class SearchResult
1415
/** @var CodeSnippet|null */
1516
public $snippet;
1617

17-
public function __construct(CodeLocation $location, ?CodeSnippet $snippet)
18+
/** @var File */
19+
protected $file;
20+
21+
/**
22+
* @param CodeLocation $location
23+
* @param CodeSnippet|null $snippet
24+
* @param File|string $file
25+
*/
26+
public function __construct(CodeLocation $location, ?CodeSnippet $snippet, $file)
1827
{
1928
$this->location = $location;
2029
$this->snippet = $snippet;
30+
$this->file = is_string($file) ? new File($file) : $file;
31+
}
32+
33+
public function file(): File
34+
{
35+
return $this->file;
2136
}
2237
}

tests/Results/SearchResultTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class SearchResultTest extends TestCase
1616
/** @test */
1717
public function it_creates_the_object_with_correct_properties()
1818
{
19+
$file = new File(tests_path('data/file2.txt'));
1920
$location = new FunctionCallLocation('my_func', 1, 1);
20-
$snippet = (new CodeSnippet())->fromFile(new File(tests_path('data/file2.txt')));
21-
$result = new SearchResult($location, $snippet);
21+
$snippet = (new CodeSnippet())->fromFile($file);
22+
$result = new SearchResult($location, $snippet, $file);
2223

2324
$this->assertMatchesObjectSnapshot($result);
2425
}

0 commit comments

Comments
 (0)