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