Skip to content

Commit b91f931

Browse files
violation can now contain a reference to the file path
1 parent e94fb42 commit b91f931

File tree

7 files changed

+214
-162
lines changed

7 files changed

+214
-162
lines changed

src/Analyzer/FileParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class FileParser implements Parser
1919

2020
private FileVisitor $fileVisitor;
2121

22-
/** @var array */
23-
private $parsingErrors;
22+
/** @var ParsingError[] */
23+
private array $parsingErrors;
2424

2525
public function __construct(
2626
NodeTraverser $traverser,

src/Analyzer/FileVisitor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class FileVisitor extends NodeVisitorAbstract
1313
{
1414
private ClassDescriptionBuilder $classDescriptionBuilder;
1515

16-
/** @var array<ClassDescription> */
16+
/** @var ClassDescription[] */
1717
private array $classDescriptions = [];
1818

1919
public function __construct(ClassDescriptionBuilder $classDescriptionBuilder)
2020
{
2121
$this->classDescriptionBuilder = $classDescriptionBuilder;
2222
}
2323

24-
public function setFilePath(string $filePath): void
24+
public function setFilePath(?string $filePath): void
2525
{
2626
$this->classDescriptionBuilder->setFilePath($filePath);
2727
}

src/Expression/ForClasses/DependsOnlyOnTheseNamespaces.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class DependsOnlyOnTheseNamespaces implements Expression
1616
{
1717
/** @var string[] */
18-
private $namespaces;
18+
private array $namespaces;
1919

2020
public function __construct(string ...$namespace)
2121
{
@@ -49,7 +49,8 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
4949
$this->describe($theClass, $because),
5050
"depends on {$dependency->getFQCN()->toString()}"
5151
),
52-
$dependency->getLine()
52+
$dependency->getLine(),
53+
$theClass->getFilePath()
5354
);
5455

5556
$violations->add($violation);

src/Expression/ForClasses/NotDependsOnTheseNamespaces.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class NotDependsOnTheseNamespaces implements Expression
1616
{
1717
/** @var string[] */
18-
private $namespaces;
18+
private array $namespaces;
1919

2020
public function __construct(string ...$namespace)
2121
{
@@ -46,7 +46,8 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
4646
$this->describe($theClass, $because),
4747
"depends on {$dependency->getFQCN()->toString()}"
4848
),
49-
$dependency->getLine()
49+
$dependency->getLine(),
50+
$theClass->getFilePath()
5051
);
5152

5253
$violations->add($violation);

src/Expression/ForClasses/NotHaveDependencyOutsideNamespace.php

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

1515
class NotHaveDependencyOutsideNamespace implements Expression
1616
{
17-
/** @var string */
18-
private $namespace;
19-
/** @var array */
20-
private $externalDependenciesToExclude;
21-
/** @var bool */
22-
private $excludeCoreNamespace;
17+
private string $namespace;
18+
19+
/** @var string[] */
20+
private array $externalDependenciesToExclude;
21+
22+
private bool $excludeCoreNamespace;
2323

2424
public function __construct(string $namespace, array $externalDependenciesToExclude = [], bool $excludeCoreNamespace = false)
2525
{
@@ -59,7 +59,8 @@ public function evaluate(ClassDescription $theClass, Violations $violations, str
5959
$this->describe($theClass, $because),
6060
"depends on {$externalDep->getFQCN()->toString()}"
6161
),
62-
$externalDep->getLine()
62+
$externalDep->getLine(),
63+
$theClass->getFilePath()
6364
);
6465
$violations->add($violation);
6566
}

src/Rules/Violation.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,26 @@ class Violation implements \JsonSerializable
1010

1111
private ?int $line;
1212

13+
private ?string $filePath;
14+
1315
private string $error;
1416

15-
public function __construct(string $fqcn, string $error, ?int $line = null)
17+
public function __construct(string $fqcn, string $error, ?int $line = null, ?string $filePath = null)
1618
{
1719
$this->fqcn = $fqcn;
1820
$this->error = $error;
1921
$this->line = $line;
22+
$this->filePath = $filePath;
2023
}
2124

2225
public static function create(string $fqcn, ViolationMessage $error): self
2326
{
2427
return new self($fqcn, $error->toString());
2528
}
2629

27-
public static function createWithErrorLine(string $fqcn, ViolationMessage $error, int $line): self
30+
public static function createWithErrorLine(string $fqcn, ViolationMessage $error, int $line, string $filePath): self
2831
{
29-
return new self($fqcn, $error->toString(), $line);
32+
return new self($fqcn, $error->toString(), $line, $filePath);
3033
}
3134

3235
public function getFqcn(): string
@@ -44,13 +47,18 @@ public function getLine(): ?int
4447
return $this->line;
4548
}
4649

50+
public function getFilePath(): ?string
51+
{
52+
return $this->filePath;
53+
}
54+
4755
public function jsonSerialize(): array
4856
{
4957
return get_object_vars($this);
5058
}
5159

5260
public static function fromJson(array $json): self
5361
{
54-
return new self($json['fqcn'], $json['error'], $json['line']);
62+
return new self($json['fqcn'], $json['error'], $json['line'], $json['filePath'] ?? null);
5563
}
5664
}

0 commit comments

Comments
 (0)