These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the ONGR package. |
||
5 | * |
||
6 | * (c) NFQ Technologies UAB <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace ONGR\TranslationsBundle\Service\Import; |
||
13 | |||
14 | use ONGR\ElasticsearchBundle\Service\Manager; |
||
15 | use ONGR\ElasticsearchBundle\Service\Repository; |
||
16 | use ONGR\ElasticsearchDSL\Query\FullText\MatchQuery; |
||
17 | use ONGR\TranslationsBundle\Document\Message; |
||
18 | use ONGR\TranslationsBundle\Document\Translation; |
||
19 | use Symfony\Component\Finder\Finder; |
||
20 | |||
21 | /** |
||
22 | * Collects translations. |
||
23 | */ |
||
24 | class ImportManager |
||
25 | { |
||
26 | /** |
||
27 | * @var FileImport |
||
28 | */ |
||
29 | private $fileImport; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $locales; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $domains; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $formats; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $translations = []; |
||
50 | |||
51 | /** |
||
52 | * @var Manager |
||
53 | */ |
||
54 | private $esManager; |
||
55 | |||
56 | /** |
||
57 | * @var Repository |
||
58 | */ |
||
59 | private $translationsRepo; |
||
60 | |||
61 | /** |
||
62 | * @param FileImport $fileImport |
||
63 | * @param Repository $repository |
||
64 | */ |
||
65 | public function __construct( |
||
66 | FileImport $fileImport, |
||
67 | $repository |
||
68 | ) { |
||
69 | $this->fileImport = $fileImport; |
||
70 | $this->translationsRepo = $repository; |
||
71 | $this->esManager = $repository->getManager(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return mixed |
||
76 | */ |
||
77 | public function getDomains() |
||
78 | { |
||
79 | return $this->domains; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param mixed $domains |
||
84 | */ |
||
85 | public function setDomains($domains) |
||
86 | { |
||
87 | $this->domains = $domains; |
||
0 ignored issues
–
show
|
|||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function getLocales() |
||
94 | { |
||
95 | return $this->locales; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param mixed $locales |
||
100 | */ |
||
101 | public function setLocales($locales) |
||
102 | { |
||
103 | $this->locales = $locales; |
||
0 ignored issues
–
show
It seems like
$locales of type * is incompatible with the declared type array of property $locales .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getFormats() |
||
110 | { |
||
111 | return $this->formats; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param mixed $formats |
||
116 | */ |
||
117 | public function setFormats($formats) |
||
118 | { |
||
119 | $this->formats = $formats; |
||
0 ignored issues
–
show
It seems like
$formats of type * is incompatible with the declared type array of property $formats .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Write translations to storage. |
||
124 | */ |
||
125 | public function writeToStorage() |
||
126 | { |
||
127 | foreach ($this->translations as $id => $translation) { |
||
128 | $search = $this->translationsRepo->createSearch(); |
||
129 | $search->addQuery(new MatchQuery('key', $id)); |
||
130 | $results = $this->translationsRepo->findDocuments($search); |
||
131 | if (count($results)) { |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | $document = new Translation(); |
||
136 | $document->setDomain($translation['domain']); |
||
137 | $document->setKey($id); |
||
138 | $document->setPath($translation['path']); |
||
139 | $document->setFormat($translation['format']); |
||
140 | foreach ($translation['messages'] as $locale => $translationMessage) { |
||
141 | $message = new Message(); |
||
142 | $message->setStatus($translationMessage['status']); |
||
143 | $message->setLocale($translationMessage['locale']); |
||
144 | $message->setMessage($translationMessage['message']); |
||
145 | $document->addMessage($message); |
||
146 | } |
||
147 | $this->esManager->persist($document); |
||
148 | } |
||
149 | |||
150 | $this->esManager->commit(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Imports translation files from a directory. |
||
155 | * @param string $dir |
||
156 | */ |
||
157 | public function importDirTranslationFiles($dir) |
||
158 | { |
||
159 | $finder = $this->findTranslationsFiles($dir); |
||
160 | $this->importTranslationFiles($finder); |
||
0 ignored issues
–
show
It seems like
$finder defined by $this->findTranslationsFiles($dir) on line 159 can be null ; however, ONGR\TranslationsBundle\...mportTranslationFiles() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
Loading history...
|
|||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Imports translation files form all bundles. |
||
165 | * |
||
166 | * @param array $bundles |
||
167 | * @param bool $isBundle |
||
168 | */ |
||
169 | public function importBundlesTranslationFiles($bundles, $isBundle = false) |
||
170 | { |
||
171 | foreach ($bundles as $bundle) { |
||
172 | $dir = $isBundle? |
||
173 | dir($bundle->getPath())->path : |
||
0 ignored issues
–
show
The property
path does not seem to exist in Directory .
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.
Loading history...
|
|||
174 | dirname((new \ReflectionClass($bundle))->getFileName()); |
||
175 | |||
176 | $this->importDirTranslationFiles($dir); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Return a Finder object if $path has a Resources/translations folder. |
||
182 | * |
||
183 | * @param string $path |
||
184 | * |
||
185 | * @return Finder |
||
186 | */ |
||
187 | protected function findTranslationsFiles($path) |
||
188 | { |
||
189 | $finder = null; |
||
190 | |||
191 | if (preg_match('#^win#i', PHP_OS)) { |
||
192 | $path = preg_replace('#' . preg_quote(DIRECTORY_SEPARATOR, '#') . '#', '/', $path); |
||
193 | } else { |
||
194 | $path = str_replace('\\', '/', $path); |
||
195 | } |
||
196 | |||
197 | $dir = $path . '/Resources/translations'; |
||
198 | |||
199 | if (is_dir($dir)) { |
||
200 | $finder = new Finder(); |
||
201 | $finder->files() |
||
202 | ->name($this->getFileNamePattern()) |
||
203 | ->in($dir); |
||
204 | } |
||
205 | |||
206 | return (null !== $finder && $finder->count() > 0) ? $finder : null; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function getFileNamePattern() |
||
213 | { |
||
214 | if (count($this->getDomains())) { |
||
215 | $regex = sprintf( |
||
216 | '/((%s)\.(%s)\.(%s))/', |
||
217 | implode('|', $this->getDomains()), |
||
218 | implode('|', $this->getLocales()), |
||
219 | implode('|', $this->getFormats()) |
||
220 | ); |
||
221 | } else { |
||
222 | $regex = sprintf( |
||
223 | '/(.*\.(%s)\.(%s))/', |
||
224 | implode('|', $this->getLocales()), |
||
225 | implode('|', $this->getFormats()) |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | return $regex; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Imports some translations files. |
||
234 | * |
||
235 | * @param Finder $finder |
||
236 | */ |
||
237 | protected function importTranslationFiles($finder) |
||
238 | { |
||
239 | if ($finder instanceof Finder) { |
||
240 | foreach ($finder as $file) { |
||
241 | $this->translations = array_replace_recursive($this->fileImport->import($file), $this->translations); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..