These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\NodeBundle\Helper; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
||
7 | use Kunstmaan\NodeBundle\Validation\URLValidator; |
||
8 | use Psr\Log\LoggerInterface; |
||
9 | use Symfony\Component\Routing\RouterInterface; |
||
10 | |||
11 | /** |
||
12 | * A helper for replacing url's |
||
13 | */ |
||
14 | class URLHelper |
||
15 | { |
||
16 | use URLValidator; |
||
17 | |||
18 | /** |
||
19 | * @var EntityManager |
||
20 | */ |
||
21 | private $em; |
||
22 | |||
23 | /** |
||
24 | * @var RouterInterface |
||
25 | */ |
||
26 | private $router; |
||
27 | |||
28 | /** |
||
29 | * @var LoggerInterface |
||
30 | */ |
||
31 | private $logger; |
||
32 | |||
33 | /** @var array */ |
||
34 | private $nodeTranslationCache = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | private $mediaCache = []; |
||
38 | |||
39 | /** |
||
40 | * @var DomainConfigurationInterface |
||
41 | */ |
||
42 | private $domainConfiguration; |
||
43 | |||
44 | 3 | public function __construct(EntityManager $em, RouterInterface $router, LoggerInterface $logger, DomainConfigurationInterface $domainConfiguration) |
|
45 | { |
||
46 | 3 | $this->em = $em; |
|
47 | 3 | $this->router = $router; |
|
48 | 3 | $this->logger = $logger; |
|
49 | 3 | $this->domainConfiguration = $domainConfiguration; |
|
50 | 3 | } |
|
51 | |||
52 | /** |
||
53 | * Replace a given text, according to the node translation id and the multidomain site id. |
||
54 | * |
||
55 | * @param string $text |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 3 | public function replaceUrl($text) |
|
60 | { |
||
61 | 3 | if ($this->isEmailAddress($text)) { |
|
62 | 1 | $text = sprintf('%s:%s', 'mailto', $text); |
|
63 | } |
||
64 | |||
65 | 3 | if ($this->isInternalLink($text)) { |
|
66 | 1 | preg_match_all("/\[(([a-z_A-Z\.]+):)?NT([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); |
|
67 | |||
68 | 1 | if (\count($matches) > 0) { |
|
69 | 1 | foreach ($matches as $match) { |
|
0 ignored issues
–
show
|
|||
70 | 1 | $fullTag = $match[0]; |
|
71 | 1 | $hostId = $match[2]; |
|
72 | |||
73 | 1 | $hostConfig = !empty($hostId) ? $this->domainConfiguration->getFullHostById($hostId) : null; |
|
74 | 1 | $host = null !== $hostConfig && array_key_exists('host', $hostConfig) ? $hostConfig['host'] : null; |
|
75 | 1 | $hostBaseUrl = $this->domainConfiguration->getHostBaseUrl($host); |
|
76 | |||
77 | 1 | $nodeTranslationId = $match[3]; |
|
78 | 1 | $nodeTranslation = $this->getNodeTranslation($nodeTranslationId); |
|
79 | |||
80 | 1 | if ($nodeTranslation) { |
|
81 | 1 | $urlParams = ['url' => $nodeTranslation['url']]; |
|
82 | // Only add locale if multilingual site |
||
83 | 1 | if ($this->domainConfiguration->isMultiLanguage($host)) { |
|
84 | $urlParams['_locale'] = $nodeTranslation['lang']; |
||
85 | } |
||
86 | |||
87 | // Only add other site, when having this. |
||
88 | 1 | if ($hostId) { |
|
89 | $urlParams['otherSite'] = $hostId; |
||
90 | } |
||
91 | |||
92 | 1 | $url = $this->router->generate('_slug', $urlParams); |
|
93 | |||
94 | 1 | $text = str_replace($fullTag, $hostId ? $hostBaseUrl . $url : $url, $text); |
|
95 | } else { |
||
96 | $this->logger->error('No NodeTranslation found in the database when replacing url tag ' . $fullTag); |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | 3 | if ($this->isInternalMediaLink($text)) { |
|
103 | 1 | preg_match_all("/\[(([a-z_A-Z]+):)?M([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); |
|
104 | |||
105 | 1 | if (\count($matches) > 0) { |
|
106 | 1 | foreach ($matches as $match) { |
|
0 ignored issues
–
show
The expression
$matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
![]() |
|||
107 | 1 | $fullTag = $match[0]; |
|
108 | 1 | $mediaId = $match[3]; |
|
109 | |||
110 | 1 | $mediaItem = $this->getMedia($mediaId); |
|
111 | 1 | if ($mediaItem) { |
|
112 | 1 | $text = str_replace($fullTag, $mediaItem['url'], $text); |
|
113 | } else { |
||
114 | $this->logger->error('No Media found in the database when replacing url tag ' . $fullTag); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | 3 | return $text; |
|
121 | } |
||
122 | |||
123 | 1 | private function getNodeTranslation($nodeTranslationId): array |
|
124 | { |
||
125 | 1 | if (isset($this->nodeTranslationCache[$nodeTranslationId])) { |
|
126 | 1 | return $this->nodeTranslationCache[$nodeTranslationId]; |
|
127 | } |
||
128 | |||
129 | 1 | $stmt = $this->em->getConnection()->executeQuery( |
|
130 | 1 | 'SELECT url, lang FROM kuma_node_translations WHERE id = :nodeTranslationId', |
|
131 | 1 | ['nodeTranslationId' => $nodeTranslationId] |
|
132 | ); |
||
133 | 1 | $nodeTranslation = $stmt->fetch(); |
|
134 | |||
135 | 1 | $this->nodeTranslationCache[$nodeTranslationId] = $nodeTranslation; |
|
136 | |||
137 | 1 | return $nodeTranslation; |
|
138 | } |
||
139 | |||
140 | 1 | private function getMedia($mediaId): array |
|
141 | { |
||
142 | 1 | if (isset($this->mediaCache[$mediaId])) { |
|
143 | 1 | return $this->mediaCache[$mediaId]; |
|
144 | } |
||
145 | |||
146 | 1 | $stmt = $this->em->getConnection()->executeQuery( |
|
147 | 1 | 'SELECT url FROM kuma_media WHERE id = :mediaId', |
|
148 | 1 | ['mediaId' => $mediaId] |
|
149 | ); |
||
150 | 1 | $media = $stmt->fetch(); |
|
151 | |||
152 | 1 | $this->mediaCache[$mediaId] = $media; |
|
153 | |||
154 | 1 | return $media; |
|
155 | } |
||
156 | } |
||
157 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.