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 PHP Translation package. |
||
5 | * |
||
6 | * (c) PHP Translation team <[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 Translation\Extractor\Visitor\Php\Symfony; |
||
13 | |||
14 | use PhpParser\Node; |
||
15 | use PhpParser\NodeVisitor; |
||
16 | |||
17 | /** |
||
18 | * @author Rein Baarsma <[email protected]> |
||
19 | */ |
||
20 | final class FormTypeLabelImplicit extends AbstractFormType implements NodeVisitor |
||
21 | { |
||
22 | use FormTrait; |
||
23 | |||
24 | 3 | public function enterNode(Node $node) |
|
25 | { |
||
26 | 3 | if (!$this->isFormType($node)) { |
|
27 | 3 | return; |
|
28 | } |
||
29 | |||
30 | 3 | parent::enterNode($node); |
|
31 | |||
32 | 3 | $domain = null; |
|
33 | // use add() function and look at first argument and if that's a string |
||
34 | 3 | if ($node instanceof Node\Expr\MethodCall |
|
35 | 3 | && (!is_object($node->name) || method_exists($node->name, '__toString')) |
|
36 | 3 | && ('add' === (string) $node->name || 'create' === (string) $node->name) |
|
37 | 3 | && $node->args[0]->value instanceof Node\Scalar\String_) { |
|
38 | 3 | $skipLabel = false; |
|
39 | // Check if the form type is "hidden" |
||
40 | 3 | if (count($node->args) >= 2) { |
|
41 | 3 | $type = $node->args[1]->value; |
|
42 | 3 | if ($type instanceof Node\Scalar\String_ && 'Symfony\Component\Form\Extension\Core\Type\HiddenType' === $type->value |
|
43 | 3 | || $type instanceof Node\Expr\ClassConstFetch && 'HiddenType' === $type->class->parts[0]) { |
|
44 | 1 | $skipLabel = true; |
|
45 | } |
||
46 | } |
||
47 | |||
48 | // now make sure we don't have 'label' in the array of options |
||
49 | 3 | if (count($node->args) >= 3) { |
|
50 | 3 | if ($node->args[2]->value instanceof Node\Expr\Array_) { |
|
51 | 3 | foreach ($node->args[2]->value->items as $item) { |
|
52 | 3 | if (isset($item->key) && $item->key instanceof Node\Scalar\String_ && 'label' === $item->key->value) { |
|
53 | 3 | $skipLabel = true; |
|
54 | } |
||
55 | |||
56 | 3 | if (isset($item->key) && $item->key instanceof Node\Scalar\String_ && 'translation_domain' === $item->key->value) { |
|
57 | 2 | if ($item->value instanceof Node\Scalar\String_) { |
|
58 | 2 | $domain = $item->value->value; |
|
59 | 2 | } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) { |
|
60 | 2 | $domain = false; |
|
61 | } |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | /* |
||
66 | * Actually there's another case here.. if the 3rd argument is anything else, it could well be |
||
67 | * that label is set through a static array. This will not be a common use-case so yeah in this case |
||
68 | * it may be the translation is double. |
||
69 | */ |
||
70 | } |
||
71 | |||
72 | // only if no custom label was found, proceed |
||
73 | 3 | if (false === $skipLabel && false !== $domain) { |
|
74 | /* |
||
75 | * Pass DocComment (if available) from first argument (name of Form field) allowing usage of Ignore |
||
76 | * annotation to disable implicit add; use case: when form options are generated by external method. |
||
77 | */ |
||
78 | 3 | if ($node->args[0]->getDocComment()) { |
|
79 | 2 | $node->setDocComment($node->args[0]->getDocComment()); |
|
80 | } |
||
81 | |||
82 | 3 | $label = $node->args[0]->value->value; |
|
83 | 3 | View Code Duplication | if (!empty($label)) { |
0 ignored issues
–
show
|
|||
84 | 3 | if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain])) { |
|
85 | 3 | $this->lateCollect($location); |
|
86 | } |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | 3 | } |
|
91 | } |
||
92 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.