Skip to content

Commit bef1df4

Browse files
LinksRule for checking validity of links
1 parent 6ec9016 commit bef1df4

27 files changed

+1232
-0
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"require-dev": {
2121
"nette/application": "^3.0",
22+
"nette/di": "^2.3.0 || ^3.0.0",
2223
"nette/forms": "^3.0",
2324
"nette/utils": "^2.3.0 || ^3.0.0",
2425
"nikic/php-parser": "^4.13.2",

extension.neon

+27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
parameters:
2+
nette:
3+
containerLoader: null
4+
applicationMapping: []
5+
checkLinks: false
26
additionalConstructors:
37
- Nette\Application\UI\Presenter::startup
48
exceptions:
@@ -48,7 +52,30 @@ parameters:
4852
- terminate
4953
- forward
5054

55+
parametersSchema:
56+
nette: structure([
57+
containerLoader: schema(string(), nullable())
58+
applicationMapping: arrayOf(string(), string())
59+
checkLinks: bool()
60+
])
61+
5162
services:
63+
netteContainerResolver:
64+
class: PHPStan\Nette\ContainerResolver
65+
arguments:
66+
- %nette.containerLoader%
67+
68+
nettePresenterResolver:
69+
class: PHPStan\Nette\PresenterResolver
70+
arguments:
71+
- %nette.applicationMapping%
72+
73+
-
74+
class: PHPStan\Nette\LinkChecker
75+
76+
-
77+
class: PHPStan\Reflection\Nette\HtmlClassReflectionExtension
78+
5279
-
5380
class: PHPStan\Reflection\Nette\HtmlClassReflectionExtension
5481
tags:

rules.neon

+5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ parametersSchema:
1616

1717
rules:
1818
- PHPStan\Rule\Nette\DoNotExtendNetteObjectRule
19+
- PHPStan\Rule\Nette\LinksRule
1920

2021
conditionalTags:
2122
PHPStan\Rule\Nette\RegularExpressionPatternRule:
2223
phpstan.rules.rule: %featureToggles.bleedingEdge%
24+
PHPStan\Rule\Nette\LinksRule:
25+
phpstan.rules.rule: %nette.checkLinks%
2326

2427
services:
2528
-
@@ -30,3 +33,5 @@ services:
3033
- phpstan.rules.rule
3134
-
3235
class: PHPStan\Rule\Nette\RegularExpressionPatternRule
36+
-
37+
class: PHPStan\Rule\Nette\LinksRule
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Exceptions;
4+
5+
use Throwable;
6+
use function sprintf;
7+
8+
class InvalidLinkDestinationException extends InvalidLinkException
9+
{
10+
11+
/** @var string */
12+
private $destination;
13+
14+
public function __construct(string $destination, int $code = 0, ?Throwable $previous = null)
15+
{
16+
parent::__construct(sprintf("Invalid link destination '%s'", $destination), $code, $previous);
17+
$this->destination = $destination;
18+
}
19+
20+
public function getDestination(): string
21+
{
22+
return $this->destination;
23+
}
24+
25+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Exceptions;
4+
5+
use RuntimeException;
6+
7+
class InvalidLinkException extends RuntimeException
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Exceptions;
4+
5+
class InvalidLinkParamsException extends InvalidLinkException
6+
{
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Exceptions;
4+
5+
use RuntimeException;
6+
7+
class LinkCheckFailedException extends RuntimeException
8+
{
9+
10+
}

src/Nette/ContainerResolver.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Nette;
4+
5+
use Nette\DI\Container;
6+
use PHPStan\ShouldNotHappenException;
7+
use function is_file;
8+
use function is_readable;
9+
use function sprintf;
10+
11+
class ContainerResolver
12+
{
13+
14+
/** @var string|null */
15+
private $containerLoader;
16+
17+
/** @var Container|false|null */
18+
private $container;
19+
20+
public function __construct(?string $containerLoader)
21+
{
22+
$this->containerLoader = $containerLoader;
23+
}
24+
25+
public function getContainer(): ?Container
26+
{
27+
if ($this->container === false) {
28+
return null;
29+
}
30+
31+
if ($this->container !== null) {
32+
return $this->container;
33+
}
34+
35+
if ($this->containerLoader === null) {
36+
$this->container = false;
37+
38+
return null;
39+
}
40+
41+
$this->container = $this->loadContainer($this->containerLoader);
42+
43+
return $this->container;
44+
}
45+
46+
47+
private function loadContainer(string $containerLoader): ?Container
48+
{
49+
if (!is_file($containerLoader)) {
50+
throw new ShouldNotHappenException(sprintf(
51+
'Nette container could not be loaded: file "%s" does not exist',
52+
$containerLoader
53+
));
54+
}
55+
56+
if (!is_readable($containerLoader)) {
57+
throw new ShouldNotHappenException(sprintf(
58+
'Nette container could not be loaded: file "%s" is not readable',
59+
$containerLoader
60+
));
61+
}
62+
63+
return require $containerLoader;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)