Conditions | 16 |
Paths | 1858 |
Total Lines | 113 |
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 |
||
98 | public function createPage(HasNodeInterface $pageTypeInstance, array $translations, array $options = array()) |
||
99 | { |
||
100 | if (is_null($options)) { |
||
101 | $options = array(); |
||
102 | } |
||
103 | |||
104 | if (is_null($translations) || (count($translations) == 0)) { |
||
105 | throw new \InvalidArgumentException('There has to be at least 1 translation in the translations array'); |
||
106 | } |
||
107 | |||
108 | $em = $this->entityManager; |
||
109 | |||
110 | /** @var NodeRepository $nodeRepo */ |
||
111 | $nodeRepo = $em->getRepository('KunstmaanNodeBundle:Node'); |
||
112 | /** @var $userRepo UserRepository */ |
||
113 | $userRepo = $em->getRepository($this->userEntityClass); |
||
114 | /** @var $seoRepo SeoRepository */ |
||
115 | try { |
||
116 | $seoRepo = $em->getRepository('KunstmaanSeoBundle:Seo'); |
||
117 | } catch (ORMException $e) { |
||
118 | $seoRepo = null; |
||
119 | } |
||
120 | |||
121 | $pagecreator = array_key_exists('creator', $options) ? $options['creator'] : 'pagecreator'; |
||
122 | $creator = $userRepo->findOneBy(array('username' => $pagecreator)); |
||
123 | |||
124 | $parent = isset($options['parent']) ? $options['parent'] : null; |
||
125 | |||
126 | $pageInternalName = isset($options['page_internal_name']) ? $options['page_internal_name'] : null; |
||
127 | |||
128 | $setOnline = isset($options['set_online']) ? $options['set_online'] : false; |
||
129 | |||
130 | // We need to get the language of the first translation so we can create the rootnode. |
||
131 | // This will also create a translationnode for that language attached to the rootnode. |
||
132 | $first = true; |
||
133 | $rootNode = null; |
||
134 | |||
135 | /* @var \Kunstmaan\NodeBundle\Repository\NodeTranslationRepository $nodeTranslationRepo*/ |
||
136 | $nodeTranslationRepo = $em->getRepository('KunstmaanNodeBundle:NodeTranslation'); |
||
137 | |||
138 | foreach ($translations as $translation) { |
||
139 | $language = $translation['language']; |
||
140 | $callback = $translation['callback']; |
||
141 | |||
142 | $translationNode = null; |
||
143 | if ($first) { |
||
144 | $first = false; |
||
145 | |||
146 | $em->persist($pageTypeInstance); |
||
147 | $em->flush($pageTypeInstance); |
||
148 | |||
149 | // Fetch the translation instead of creating it. |
||
150 | // This returns the rootnode. |
||
151 | $rootNode = $nodeRepo->createNodeFor($pageTypeInstance, $language, $creator, $pageInternalName); |
||
152 | |||
153 | if (array_key_exists('hidden_from_nav', $options)) { |
||
154 | $rootNode->setHiddenFromNav($options['hidden_from_nav']); |
||
155 | } |
||
156 | |||
157 | if (!is_null($parent)) { |
||
158 | if ($parent instanceof HasPagePartsInterface) { |
||
159 | $parent = $nodeRepo->getNodeFor($parent); |
||
160 | } |
||
161 | $rootNode->setParent($parent); |
||
162 | } |
||
163 | |||
164 | $em->persist($rootNode); |
||
165 | $em->flush($rootNode); |
||
166 | |||
167 | $translationNode = $rootNode->getNodeTranslation($language, true); |
||
168 | } else { |
||
169 | // Clone the $pageTypeInstance. |
||
170 | $pageTypeInstance = clone $pageTypeInstance; |
||
171 | |||
172 | $em->persist($pageTypeInstance); |
||
173 | $em->flush($pageTypeInstance); |
||
174 | |||
175 | // Create the translationnode. |
||
176 | $translationNode = $nodeTranslationRepo->createNodeTranslationFor($pageTypeInstance, $language, $rootNode, $creator); |
||
177 | } |
||
178 | |||
179 | // Make SEO. |
||
180 | $seo = null; |
||
181 | |||
182 | if (!is_null($seoRepo)) { |
||
183 | $seo = $seoRepo->findOrCreateFor($pageTypeInstance); |
||
184 | } |
||
185 | |||
186 | $callback($pageTypeInstance, $translationNode, $seo); |
||
187 | |||
188 | // Overwrite the page title with the translated title |
||
189 | $pageTypeInstance->setTitle($translationNode->getTitle()); |
||
190 | $em->persist($pageTypeInstance); |
||
191 | $em->persist($translationNode); |
||
192 | $em->flush($pageTypeInstance); |
||
193 | $em->flush($translationNode); |
||
194 | |||
195 | $translationNode->setOnline($setOnline); |
||
196 | |||
197 | if (!is_null($seo)) { |
||
198 | $em->persist($seo); |
||
199 | $em->flush($seo); |
||
200 | } |
||
201 | |||
202 | $em->persist($translationNode); |
||
203 | $em->flush($translationNode); |
||
204 | } |
||
205 | |||
206 | // ACL |
||
207 | $this->aclPermissionCreatorService->createPermission($rootNode); |
||
208 | |||
209 | return $rootNode; |
||
210 | } |
||
211 | |||
213 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.