1 | <?php namespace Modules\Core\Internationalisation; |
||
6 | abstract class BaseFormRequest extends FormRequest |
||
7 | { |
||
8 | /** |
||
9 | * Set the translation key prefix for attributes. |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $translationsAttributesKey = 'validation.attributes.'; |
||
13 | /** |
||
14 | * Current processed locale |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $localeKey; |
||
18 | |||
19 | /** |
||
20 | * Return an array of rules for translatable fields |
||
21 | * @return array |
||
22 | */ |
||
23 | public function translationRules() |
||
27 | |||
28 | /** |
||
29 | * Return an array of messages for translatable fields |
||
30 | * @return array |
||
31 | */ |
||
32 | public function translationMessages() |
||
36 | |||
37 | /** |
||
38 | * Get the validator instance for the request. |
||
39 | * @return \Illuminate\Validation\Validator |
||
40 | */ |
||
41 | protected function getValidatorInstance() |
||
42 | { |
||
43 | $factory = $this->container->make('Illuminate\Validation\Factory'); |
||
44 | if (method_exists($this, 'validator')) { |
||
45 | return $this->container->call([$this, 'validator'], compact('factory')); |
||
46 | } |
||
47 | |||
48 | $rules = $this->container->call([$this, 'rules']); |
||
49 | $attributes = $this->attributes(); |
||
50 | $messages = []; |
||
51 | |||
52 | $translationsAttributesKey = $this->getTranslationsAttributesKey(); |
||
53 | |||
54 | foreach ($this->requiredLocales() as $localeKey => $locale) { |
||
55 | $this->localeKey = $localeKey; |
||
|
|||
56 | foreach ($this->container->call([$this, 'translationRules']) as $attribute => $rule) { |
||
57 | $key = $localeKey . '.' . $attribute; |
||
58 | $rules[$key] = $rule; |
||
59 | $attributes[$key] = trans($translationsAttributesKey . $attribute); |
||
60 | } |
||
61 | |||
62 | foreach ($this->container->call([$this, 'translationMessages']) as $attributeAndRule => $message) { |
||
63 | $messages[$localeKey . '.' . $attributeAndRule] = $message; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $factory->make( |
||
68 | $this->all(), $rules, array_merge($this->messages(), $messages), $attributes |
||
69 | ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | public function withTranslations() |
||
88 | |||
89 | /** |
||
90 | * @return \Illuminate\Support\Collection |
||
91 | */ |
||
92 | public function requiredLocales() |
||
96 | |||
97 | /** |
||
98 | * Get the validation for attributes key from the implementing class |
||
99 | * or use a sensible default |
||
100 | * @return string |
||
101 | */ |
||
102 | private function getTranslationsAttributesKey() |
||
106 | } |
||
107 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.