Conditions | 8 |
Paths | 30 |
Total Lines | 81 |
Code Lines | 51 |
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 |
||
82 | public function createService(ServiceLocatorInterface $serviceLocator) |
||
83 | { |
||
84 | /* @var $serviceLocator \Zend\Form\FormElementManager */ |
||
85 | $service = $serviceLocator->getServiceLocator(); |
||
86 | $options=null; |
||
87 | if ($this->options) { |
||
|
|||
88 | $options = $service->get($this->options); |
||
89 | } |
||
90 | |||
91 | // Retrieve configuration. |
||
92 | if ($this->configKey) { |
||
93 | $services = $serviceLocator->getServiceLocator(); |
||
94 | $config = $services->get('config'); |
||
95 | $config = isset($config['form_elements_config']['file_upload_factories'][$this->configKey]) |
||
96 | ? $config['form_elements_config']['file_upload_factories'][$this->configKey] |
||
97 | : array(); |
||
98 | $this->config = $config; |
||
99 | } |
||
100 | |||
101 | |||
102 | $form = new Form(); |
||
103 | $serviceLocator->injectFactory($form, $serviceLocator); |
||
104 | $form->add( |
||
105 | array( |
||
106 | 'type' => $this->fileElement, |
||
107 | 'name' => $this->fileName, |
||
108 | 'options' => array( |
||
109 | 'use_formrow_helper' => false, |
||
110 | ), |
||
111 | 'attributes' => array( |
||
112 | 'class' => 'hide', |
||
113 | ), |
||
114 | ) |
||
115 | ); |
||
116 | /* @var $element \Core\Form\Element\FileUpload */ |
||
117 | $element = $form->get($this->fileName); |
||
118 | $element->setIsMultiple($this->multiple); |
||
119 | |||
120 | $user = $serviceLocator->getServiceLocator()->get('AuthenticationService')->getUser(); |
||
121 | |||
122 | if (isset($this->config['hydrator']) && $this->config['hydrator']) { |
||
123 | /** @noinspection PhpUndefinedVariableInspection */ |
||
124 | $hydrator = $services->get('HydratorManager')->get($this->config['hydrator']); |
||
125 | } else { |
||
126 | |||
127 | |||
128 | /* @var $fileEntity \Core\Entity\FileInterface */ |
||
129 | $fileEntity = new $this->fileEntityClass(); |
||
130 | if ($user instanceof AnonymousUser) { |
||
131 | $fileEntity->getPermissions()->grant($user, 'all'); |
||
132 | } else { |
||
133 | $fileEntity->setUser($user); |
||
134 | } |
||
135 | |||
136 | $strategy = new FileUploadStrategy($fileEntity); |
||
137 | if ($this->multiple) { |
||
138 | $hydrator = new FileCollectionUploadHydrator($this->fileName, $strategy); |
||
139 | $form->add( |
||
140 | array( |
||
141 | 'type' => 'button', |
||
142 | 'name' => 'remove', |
||
143 | 'options' => array( |
||
144 | 'label' => /*@translate*/ 'Remove all', |
||
145 | ), |
||
146 | 'attributes' => array( |
||
147 | 'class' => 'fu-remove-all btn btn-danger btn-xs pull-right' |
||
148 | ), |
||
149 | ) |
||
150 | ); |
||
151 | } else { |
||
152 | $hydrator = new EntityHydrator(); |
||
153 | $hydrator->addStrategy($this->fileName, $strategy); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | $form->setHydrator($hydrator); |
||
158 | $form->setOptions(array('use_files_array' => true)); |
||
159 | |||
160 | $this->configureForm($form, $options); |
||
161 | return $form; |
||
162 | } |
||
163 | |||
174 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: