Conditions | 16 |
Paths | 8 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function mapFormsToData($forms, &$data): void |
||
70 | { |
||
71 | if (null === $data) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | if (!is_array($data) && !is_object($data)) { |
||
76 | throw new UnexpectedTypeException($data, 'object, array or empty'); |
||
77 | } |
||
78 | |||
79 | foreach ($forms as $form) { |
||
80 | $propertyPath = $form->getPropertyPath(); |
||
81 | $config = $form->getConfig(); |
||
82 | |||
83 | // Write-back is disabled if the form is not synchronized (transformation failed), |
||
84 | // if the form was not submitted and if the form is disabled (modification not allowed) |
||
85 | if ( |
||
86 | null === $propertyPath || |
||
87 | !$config->getMapped() || |
||
88 | !$form->isSubmitted() || |
||
89 | !$form->isSynchronized() || |
||
90 | $form->isDisabled() |
||
91 | ) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | // If $data is out ContentCreateStruct, we need to map it to the corresponding field |
||
96 | // in the struct |
||
97 | if ($data instanceof DataWrapper) { |
||
98 | /* @var $data \Netgen\Bundle\EzFormsBundle\Form\DataWrapper */ |
||
99 | $this->mapFromForm($form, $data, $propertyPath); |
||
100 | |||
101 | continue; |
||
102 | } |
||
103 | |||
104 | // If the field is of type DateTime and the data is the same skip the update to |
||
105 | // keep the original object hash |
||
106 | if ( |
||
107 | $form->getData() instanceof DateTime && |
||
108 | $form->getData() === $this->propertyAccessor->getValue($data, $propertyPath) |
||
109 | ) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | // If the data is identical to the value in $data, we are |
||
114 | // dealing with a reference |
||
115 | if ( |
||
116 | is_object($data) && |
||
117 | $config->getByReference() && |
||
118 | $form->getData() === $this->propertyAccessor->getValue($data, $propertyPath) |
||
119 | ) { |
||
120 | continue; |
||
121 | } |
||
122 | |||
123 | $this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); |
||
124 | } |
||
125 | } |
||
126 | |||
165 |