Conditions | 10 |
Paths | 9 |
Total Lines | 78 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
38 | public function indexAction( |
||
39 | ZikulaHttpKernelInterface $kernel, |
||
40 | Request $request, |
||
41 | Filesystem $fileSystem, |
||
42 | RouterInterface $router, |
||
43 | AssetFilter $assetFilter, |
||
44 | string $moduleName |
||
45 | ): Response |
||
46 | { |
||
47 | $page = $request->query->get('page', 'README'); |
||
48 | if (false !== mb_strpos($page, '..')) { |
||
49 | throw new \Exception('Invalid page "' . $page . '".'); |
||
50 | } |
||
51 | |||
52 | $locale = $request->getLocale(); |
||
53 | $extension = $kernel->getBundle($moduleName); |
||
54 | if (null === $extension) { |
||
55 | throw new \Exception('Invalid extension "' . $moduleName . '".'); |
||
56 | } |
||
57 | |||
58 | $helpPath = $extension->getPath() . '/Resources/docs/help/'; |
||
59 | |||
60 | // check if requested page exists |
||
61 | if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) { |
||
62 | if ('en' === $locale) { |
||
63 | throw new \Exception('Invalid page "' . $page . '".'); |
||
64 | } |
||
65 | // fallback to English |
||
66 | $locale = 'en'; |
||
67 | } |
||
68 | if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) { |
||
69 | throw new \Exception('Invalid page "' . $page . '".'); |
||
70 | } |
||
71 | |||
72 | $raw = $request->query->getInt('raw', 0); |
||
73 | |||
74 | $content = file_get_contents($helpPath . $locale . '/' . $page . '.md'); |
||
75 | |||
76 | // rewrite local links |
||
77 | $content = preg_replace_callback( |
||
78 | '/\[(.*?)\]\((.*?)\)/', |
||
79 | function ($match) use($router, $moduleName, $raw) { |
||
80 | if (false === mb_strpos($match[2], '.md')) { |
||
81 | return $match[0]; |
||
82 | } |
||
83 | if ('http' === mb_substr($match[2], 0, 4)) { |
||
84 | return $match[0]; |
||
85 | } |
||
86 | |||
87 | // local link - rewrite |
||
88 | $urlArgs = [ |
||
89 | 'moduleName' => $moduleName, |
||
90 | 'page' => trim($match[2], '.md') |
||
91 | ]; |
||
92 | if (1 === $raw) { |
||
93 | $urlArgs['raw'] = 1; |
||
94 | } |
||
95 | $url = $router->generate('zikulaextensionsmodule_help_index', $urlArgs); |
||
96 | |||
97 | return '[' . $match[1] . '](' . $url . ')'; |
||
98 | }, |
||
99 | $content |
||
100 | ); |
||
101 | |||
102 | $output = $this->renderView('@ZikulaExtensionsModule/Help/page.html.twig', [ |
||
103 | 'moduleName' => $moduleName, |
||
104 | 'content' => $content, |
||
105 | 'raw' => $raw |
||
106 | ]); |
||
107 | |||
108 | if (1 === $raw) { |
||
109 | $output = $assetFilter->filter($output); |
||
110 | $output = new PlainResponse($output); |
||
111 | |||
112 | return $output; |
||
113 | } |
||
114 | |||
115 | return new Response($output); |
||
116 | } |
||
118 |