| Conditions | 20 |
| Paths | 50 |
| Total Lines | 85 |
| Code Lines | 51 |
| 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 |
||
| 26 | public function onKernelRequest(RequestEvent $event): void |
||
| 27 | { |
||
| 28 | if (!$event->isMainRequest() || PHP_SAPI === 'cli') { |
||
| 29 | return; |
||
| 30 | } |
||
| 31 | |||
| 32 | $request = $event->getRequest(); |
||
| 33 | $path = $request->getPathInfo(); |
||
| 34 | |||
| 35 | if ( |
||
| 36 | str_starts_with($path, '/main/install/') |
||
| 37 | || str_starts_with($path, '/_wdt') |
||
| 38 | || str_starts_with($path, '/_profiler') |
||
| 39 | || str_starts_with($path, '/build/') |
||
| 40 | || str_starts_with($path, '/bundles/') |
||
| 41 | || str_starts_with($path, '/favicon') |
||
| 42 | ) { |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | $installedFlag = (string) ( |
||
| 47 | $request->server->get('APP_INSTALLED') |
||
| 48 | ?? $_ENV['APP_INSTALLED'] |
||
| 49 | ?? getenv('APP_INSTALLED') |
||
| 50 | ?? '0' |
||
| 51 | ); |
||
| 52 | $installedFlag = trim($installedFlag, " \t\n\r\0\x0B'\""); |
||
| 53 | |||
| 54 | if ($installedFlag !== '1') { |
||
| 55 | $this->redirectToInstaller($event); |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Cache "healthy" per PHP-FPM worker |
||
| 60 | static $isHealthy = false; |
||
| 61 | if ($isHealthy) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | // Cache which settings table to use per PHP-FPM worker |
||
| 66 | static $settingsTable = null; |
||
| 67 | |||
| 68 | try { |
||
| 69 | // If DB is down/unreachable, this will fail quickly |
||
| 70 | $this->connection->executeQuery($this->connection->getDatabasePlatform()->getDummySelectSQL()); |
||
| 71 | |||
| 72 | if ($settingsTable === null) { |
||
| 73 | $settingsTable = $this->detectSettingsTable(); |
||
| 74 | if ($settingsTable === null) { |
||
| 75 | $this->redirectToInstaller($event); |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // Cheap proof: at least 1 row |
||
| 81 | $hasRow = $this->connection->fetchOne("SELECT 1 FROM {$settingsTable} LIMIT 1"); |
||
| 82 | if ($hasRow === false || $hasRow === null) { |
||
| 83 | $this->redirectToInstaller($event); |
||
| 84 | return; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Read DB version (try common column names; only runs until first success) |
||
| 88 | $version = null; |
||
| 89 | foreach (['selected_value', 'value', 'c_value'] as $col) { |
||
| 90 | try { |
||
| 91 | $version = $this->connection->fetchOne( |
||
| 92 | "SELECT {$col} FROM {$settingsTable} WHERE variable = :var LIMIT 1", |
||
| 93 | ['var' => self::VERSION_KEY] |
||
| 94 | ); |
||
| 95 | if (!empty($version)) { |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } catch (\Throwable) { |
||
| 99 | // Try next column |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | if (empty($version)) { |
||
| 104 | $this->redirectToInstaller($event); |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | $isHealthy = true; |
||
| 109 | } catch (\Throwable) { |
||
| 110 | $this->redirectToInstaller($event); |
||
| 111 | } |
||
| 132 |