Skip to content

Commit 29e5cfb

Browse files
adds relative file path to ClassDescription
1 parent 3b479d6 commit 29e5cfb

37 files changed

+143
-113
lines changed

src/Analyzer/ClassDescription.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class ClassDescription
88
{
99
private FullyQualifiedClassName $FQCN;
1010

11+
private string $filePath;
12+
1113
/** @var list<ClassDependency> */
1214
private array $dependencies;
1315

@@ -53,10 +55,12 @@ public function __construct(
5355
bool $interface,
5456
bool $trait,
5557
bool $enum,
56-
array $docBlock = [],
57-
array $attributes = []
58+
array $docBlock,
59+
array $attributes,
60+
string $filePath
5861
) {
5962
$this->FQCN = $FQCN;
63+
$this->filePath = $filePath;
6064
$this->dependencies = $dependencies;
6165
$this->interfaces = $interfaces;
6266
$this->extends = $extends;
@@ -70,10 +74,11 @@ public function __construct(
7074
$this->enum = $enum;
7175
}
7276

73-
public static function getBuilder(string $FQCN): ClassDescriptionBuilder
77+
public static function getBuilder(string $FQCN, string $relativeFilePath): ClassDescriptionBuilder
7478
{
7579
$cb = new ClassDescriptionBuilder();
7680
$cb->setClassName($FQCN);
81+
$cb->setFilePath($relativeFilePath);
7782

7883
return $cb;
7984
}
@@ -88,6 +93,11 @@ public function getFQCN(): string
8893
return $this->FQCN->toString();
8994
}
9095

96+
public function getFilePath(): string
97+
{
98+
return $this->filePath;
99+
}
100+
91101
public function namespaceMatches(string $pattern): bool
92102
{
93103
return $this->FQCN->matches($pattern);

src/Analyzer/ClassDescriptionBuilder.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ClassDescriptionBuilder
3636

3737
private bool $enum = false;
3838

39+
private ?string $filePath = null;
40+
3941
public function clear(): void
4042
{
4143
$this->FQCN = null;
@@ -52,6 +54,13 @@ public function clear(): void
5254
$this->enum = false;
5355
}
5456

57+
public function setFilePath(?string $filePath): self
58+
{
59+
$this->filePath = $filePath;
60+
61+
return $this;
62+
}
63+
5564
public function setClassName(string $FQCN): self
5665
{
5766
$this->FQCN = FullyQualifiedClassName::fromString($FQCN);
@@ -142,6 +151,7 @@ public function addAttribute(string $FQCN, int $line): self
142151
public function build(): ClassDescription
143152
{
144153
Assert::notNull($this->FQCN, 'You must set an FQCN');
154+
Assert::notNull($this->filePath, 'You must set a file path');
145155

146156
return new ClassDescription(
147157
$this->FQCN,
@@ -155,7 +165,8 @@ public function build(): ClassDescription
155165
$this->trait,
156166
$this->enum,
157167
$this->docBlock,
158-
$this->attributes
168+
$this->attributes,
169+
$this->filePath
159170
);
160171
}
161172
}

src/Analyzer/FileParser.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function parse(string $fileContent, string $filename): void
5050
$this->parsingErrors = [];
5151
try {
5252
$this->fileVisitor->clearParsedClassDescriptions();
53+
$this->fileVisitor->setFilePath($filename);
5354

5455
$errorHandler = new Collecting();
5556
$stmts = $this->parser->parse($fileContent, $errorHandler);

src/Analyzer/FileVisitor.php

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function __construct(ClassDescriptionBuilder $classDescriptionBuilder)
2121
$this->classDescriptionBuilder = $classDescriptionBuilder;
2222
}
2323

24+
public function setFilePath(string $filePath): void
25+
{
26+
$this->classDescriptionBuilder->setFilePath($filePath);
27+
}
28+
2429
public function enterNode(Node $node): void
2530
{
2631
if ($node instanceof Node\Stmt\Class_) {
@@ -224,6 +229,7 @@ public function getClassDescriptions(): array
224229
public function clearParsedClassDescriptions(): void
225230
{
226231
$this->classDescriptions = [];
232+
$this->classDescriptionBuilder->setFilePath(null);
227233
$this->classDescriptionBuilder->clear();
228234
}
229235

tests/Unit/Analyzer/ClassDescriptionBuilderTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_it_should_create_builder_with_dependency_and_interface(): v
1616
{
1717
$FQCN = 'HappyIsland';
1818
$classDescriptionBuilder = new ClassDescriptionBuilder();
19-
$classDescriptionBuilder->setClassName($FQCN);
19+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
2020

2121
$classDependency = new ClassDependency('DepClass', 10);
2222

@@ -35,7 +35,7 @@ public function test_it_should_create_final_class(): void
3535
{
3636
$FQCN = 'HappyIsland';
3737
$classDescriptionBuilder = new ClassDescriptionBuilder();
38-
$classDescriptionBuilder->setClassName($FQCN);
38+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
3939
$classDescriptionBuilder->setFinal(true);
4040

4141
$classDescription = $classDescriptionBuilder->build();
@@ -49,7 +49,7 @@ public function test_it_should_create_not_final_class(): void
4949
{
5050
$FQCN = 'HappyIsland';
5151
$classDescriptionBuilder = new ClassDescriptionBuilder();
52-
$classDescriptionBuilder->setClassName($FQCN);
52+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
5353
$classDescriptionBuilder->setFinal(false);
5454

5555
$classDescription = $classDescriptionBuilder->build();
@@ -63,7 +63,7 @@ public function test_it_should_create_abstract_class(): void
6363
{
6464
$FQCN = 'HappyIsland';
6565
$classDescriptionBuilder = new ClassDescriptionBuilder();
66-
$classDescriptionBuilder->setClassName($FQCN);
66+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
6767
$classDescriptionBuilder->setAbstract(true);
6868

6969
$classDescription = $classDescriptionBuilder->build();
@@ -77,7 +77,7 @@ public function test_it_should_create_not_abstract_class(): void
7777
{
7878
$FQCN = 'HappyIsland';
7979
$classDescriptionBuilder = new ClassDescriptionBuilder();
80-
$classDescriptionBuilder->setClassName($FQCN);
80+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
8181
$classDescriptionBuilder->setAbstract(false);
8282

8383
$classDescription = $classDescriptionBuilder->build();
@@ -91,7 +91,7 @@ public function test_it_should_create_annotated_class(): void
9191
{
9292
$FQCN = 'HappyIsland';
9393
$classDescriptionBuilder = new ClassDescriptionBuilder();
94-
$classDescriptionBuilder->setClassName($FQCN);
94+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
9595
$classDescriptionBuilder->addDocBlock('/**
9696
* @psalm-immutable
9797
*/');
@@ -112,7 +112,7 @@ public function test_it_should_add_attributes(): void
112112
{
113113
$FQCN = 'HappyIsland';
114114
$classDescriptionBuilder = new ClassDescriptionBuilder();
115-
$classDescriptionBuilder->setClassName($FQCN);
115+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
116116
$classDescriptionBuilder->addAttribute('AttrClass', 27);
117117

118118
$classDescription = $classDescriptionBuilder->build();
@@ -127,7 +127,7 @@ public function test_it_should_create_interface(): void
127127
{
128128
$FQCN = 'HappyIsland';
129129
$classDescriptionBuilder = new ClassDescriptionBuilder();
130-
$classDescriptionBuilder->setClassName($FQCN);
130+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
131131
$classDescriptionBuilder->setInterface(true);
132132

133133
$classDescription = $classDescriptionBuilder->build();
@@ -141,7 +141,7 @@ public function test_it_should_create_not_interface(): void
141141
{
142142
$FQCN = 'HappyIsland';
143143
$classDescriptionBuilder = new ClassDescriptionBuilder();
144-
$classDescriptionBuilder->setClassName($FQCN);
144+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
145145
$classDescriptionBuilder->setInterface(false);
146146

147147
$classDescription = $classDescriptionBuilder->build();
@@ -155,7 +155,7 @@ public function test_it_should_create_trait(): void
155155
{
156156
$FQCN = 'HappyIsland';
157157
$classDescriptionBuilder = new ClassDescriptionBuilder();
158-
$classDescriptionBuilder->setClassName($FQCN);
158+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
159159
$classDescriptionBuilder->setTrait(true);
160160

161161
$classDescription = $classDescriptionBuilder->build();
@@ -169,7 +169,7 @@ public function test_it_should_create_not_trait(): void
169169
{
170170
$FQCN = 'HappyIsland';
171171
$classDescriptionBuilder = new ClassDescriptionBuilder();
172-
$classDescriptionBuilder->setClassName($FQCN);
172+
$classDescriptionBuilder->setFilePath('src/Foo.php')->setClassName($FQCN);
173173
$classDescriptionBuilder->setTrait(false);
174174

175175
$classDescription = $classDescriptionBuilder->build();

tests/Unit/Analyzer/ClassDescriptionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ClassDescriptionTest extends TestCase
1515

1616
protected function setUp(): void
1717
{
18-
$this->builder = ClassDescription::getBuilder('Fruit\Banana');
18+
$this->builder = ClassDescription::getBuilder('Fruit\Banana', 'src/Foo.php');
1919
}
2020

2121
public function test_should_return_true_if_there_class_is_in_namespace(): void

tests/Unit/Analyzer/FileParserTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function test_parse_file(): void
2626
$traverser->addVisitor($nameResolver);
2727
$traverser->addVisitor($fileVisitor);
2828

29+
$fileVisitor->setFilePath('foo')->shouldBeCalled();
2930
$fileVisitor->clearParsedClassDescriptions()->shouldBeCalled();
3031

3132
$fileParser = new FileParser(
@@ -52,6 +53,7 @@ public function test_parse_file_with_name_match(): void
5253
$traverser->addVisitor($nameResolver);
5354
$traverser->addVisitor($fileVisitor);
5455

56+
$fileVisitor->setFilePath('foo')->shouldBeCalled();
5557
$fileVisitor->clearParsedClassDescriptions()->shouldBeCalled();
5658

5759
$fileParser = new FileParser(

tests/Unit/Expressions/ForClasses/ContainDocBlockLikeTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_it_should_return_true_if_contains_doc_block(): void
1616
$expression = new ContainDocBlockLike('myDocBlock');
1717

1818
$classDescription = (new ClassDescriptionBuilder())
19-
->setClassName('HappyIsland\Myclass')
19+
->setFilePath('src/Foo.php')->setClassName('HappyIsland\Myclass')
2020
->addDocBlock('/** */myDocBlock with other information')
2121
->build();
2222

@@ -36,7 +36,7 @@ public function test_it_should_return_false_if_not_contains_doc_block(): void
3636
$expression = new ContainDocBlockLike('anotherDocBlock');
3737

3838
$classDescription = (new ClassDescriptionBuilder())
39-
->setClassName('HappyIsland\Myclass')
39+
->setFilePath('src/Foo.php')->setClassName('HappyIsland\Myclass')
4040
->addDocBlock('/** */myDocBlock with other information')
4141
->build();
4242

tests/Unit/Expressions/ForClasses/DependsOnlyOnTheseNamespacesTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_it_should_return_true_if_it_has_no_dependencies(): void
1616
{
1717
$dependOnClasses = new DependsOnlyOnTheseNamespaces('myNamespace');
1818

19-
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass')->build();
19+
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass', 'src/Foo.php')->build();
2020

2121
$because = 'we want to add this rule for our software';
2222
$violations = new Violations();
@@ -33,7 +33,7 @@ public function test_it_should_return_true_if_not_depends_on_namespace(): void
3333
{
3434
$dependOnClasses = new DependsOnlyOnTheseNamespaces('myNamespace');
3535

36-
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass')
36+
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass', 'src/Foo.php')
3737
->addDependency(new ClassDependency('myNamespace\Banana', 0))
3838
->addDependency(new ClassDependency('anotherNamespace\Banana', 1))
3939
->build();
@@ -53,7 +53,7 @@ public function test_it_should_return_true_if_depends_on_class_in_root_namespace
5353
{
5454
$dependOnClasses = new DependsOnlyOnTheseNamespaces('myNamespace');
5555

56-
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass')
56+
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass', 'src/Foo.php')
5757
->addDependency(new ClassDependency('myNamespace\Banana', 0))
5858
->addDependency(new ClassDependency('\anotherNamespace\Banana', 1))
5959
->addDependency(new ClassDependency('\DateTime', 10))
@@ -71,7 +71,7 @@ public function test_it_should_return_false_if_depends_on_namespace(): void
7171
{
7272
$dependOnClasses = new DependsOnlyOnTheseNamespaces('myNamespace');
7373

74-
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass')
74+
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass', 'src/Foo.php')
7575
->addDependency(new ClassDependency('myNamespace\Banana', 0))
7676
->addDependency(new ClassDependency('myNamespace\Mango', 10))
7777
->build();
@@ -87,7 +87,7 @@ public function test_it_should_return_true_if_depends_on_same_namespace_without_
8787
{
8888
$dependOnClasses = new DependsOnlyOnTheseNamespaces();
8989

90-
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass')
90+
$classDescription = ClassDescription::getBuilder('HappyIsland\Myclass', 'src/Foo.php')
9191
->addDependency(new ClassDependency('HappyIsland\Banana', 0))
9292
->addDependency(new ClassDependency('myNamespace\Mango', 10))
9393
->build();

tests/Unit/Expressions/ForClasses/ExtendTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_it_should_return_no_violation_on_success(): void
1616
$extend = new Extend('My\BaseClass');
1717

1818
$classDescription = (new ClassDescriptionBuilder())
19-
->setClassName('My\Class')
19+
->setFilePath('src/Foo.php')->setClassName('My\Class')
2020
->addExtends('My\BaseClass', 10)
2121
->build();
2222

@@ -31,7 +31,7 @@ public function test_it_should_work_with_wildcards(): void
3131
$extend = new Extend('My\B14*');
3232

3333
$classDescription = (new ClassDescriptionBuilder())
34-
->setClassName('My\Class')
34+
->setFilePath('src/Foo.php')->setClassName('My\Class')
3535
->addExtends('My\B14Class', 10)
3636
->build();
3737

@@ -46,7 +46,7 @@ public function test_it_should_return_violation_error_when_argument_is_a_regex()
4646
$extend = new Extend('App\Providers\(Auth|Event|Route|Horizon)ServiceProvider');
4747

4848
$classDescription = (new ClassDescriptionBuilder())
49-
->setClassName('My\Class')
49+
->setFilePath('src/Foo.php')->setClassName('My\Class')
5050
->addExtends('My\BaseClass', 10)
5151
->build();
5252

@@ -60,7 +60,7 @@ public function test_it_should_return_violation_error_when_class_not_extend(): v
6060
$extend = new Extend('My\BaseClass');
6161

6262
$classDescription = (new ClassDescriptionBuilder())
63-
->setClassName('HappyIsland')
63+
->setFilePath('src/Foo.php')->setClassName('HappyIsland')
6464
->addExtends('My\AnotherClass', 10)
6565
->build();
6666

@@ -76,7 +76,7 @@ public function test_it_should_return_violation_error_if_extend_is_null(): void
7676
$extend = new Extend('My\BaseClass');
7777

7878
$classDescription = (new ClassDescriptionBuilder())
79-
->setClassName('HappyIsland')
79+
->setFilePath('src/Foo.php')->setClassName('HappyIsland')
8080
->build();
8181

8282
$because = 'we want to add this rule for our software';
@@ -94,7 +94,7 @@ public function test_it_should_accept_multiple_extends(): void
9494
$extend = new Extend('My\FirstExtend', 'My\SecondExtend');
9595

9696
$classDescription = (new ClassDescriptionBuilder())
97-
->setClassName('My\Class')
97+
->setFilePath('src/Foo.php')->setClassName('My\Class')
9898
->addExtends('My\SecondExtend', 10)
9999
->build();
100100

tests/Unit/Expressions/ForClasses/HaveAttributeTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function test_it_should_return_true_if_contains_doc_block(): void
1616
$expression = new HaveAttribute('myAttribute');
1717

1818
$classDescription = (new ClassDescriptionBuilder())
19-
->setClassName('HappyIsland\Myclass')
19+
->setFilePath('src/Foo.php')->setClassName('HappyIsland\Myclass')
2020
->addAttribute('myAttribute', 1)
2121
->build();
2222

@@ -36,7 +36,7 @@ public function test_it_should_return_true_if_contains_doc_block_without_because
3636
$expression = new HaveAttribute('myAttribute');
3737

3838
$classDescription = (new ClassDescriptionBuilder())
39-
->setClassName('HappyIsland\Myclass')
39+
->setFilePath('src/Foo.php')->setClassName('HappyIsland\Myclass')
4040
->addAttribute('myAttribute', 1)
4141
->build();
4242

@@ -55,7 +55,7 @@ public function test_it_should_return_false_if_not_contains_doc_block(): void
5555
$expression = new HaveAttribute('anotherAttribute');
5656

5757
$classDescription = (new ClassDescriptionBuilder())
58-
->setClassName('HappyIsland\Myclass')
58+
->setFilePath('src/Foo.php')->setClassName('HappyIsland\Myclass')
5959
->addAttribute('myAttribute', 1)
6060
->build();
6161

0 commit comments

Comments
 (0)