| Conditions | 14 |
| Paths | 105 |
| Total Lines | 65 |
| Code Lines | 38 |
| 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 |
||
| 75 | public function start() { |
||
| 76 | // Trap SIGTERM |
||
| 77 | pcntl_signal( SIGTERM, [ $this, 'handleTermSignal' ], false ); |
||
| 78 | |||
| 79 | do { |
||
| 80 | // Start child processes |
||
| 81 | if ( $this->procsToStart ) { |
||
| 82 | if ( $this->forkWorkers( $this->procsToStart ) == 'child' ) { |
||
| 83 | return 'child'; |
||
| 84 | } |
||
| 85 | $this->procsToStart = 0; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Check child status |
||
| 89 | $status = false; |
||
| 90 | $deadPid = pcntl_wait( $status ); |
||
| 91 | |||
| 92 | if ( $deadPid > 0 ) { |
||
| 93 | // Respond to child process termination |
||
| 94 | unset( $this->children[$deadPid] ); |
||
| 95 | if ( $this->flags & self::RESTART_ON_ERROR ) { |
||
| 96 | if ( pcntl_wifsignaled( $status ) ) { |
||
| 97 | // Restart if the signal was abnormal termination |
||
| 98 | // Don't restart if it was deliberately killed |
||
| 99 | $signal = pcntl_wtermsig( $status ); |
||
| 100 | if ( in_array( $signal, self::$restartableSignals ) ) { |
||
| 101 | echo "Worker exited with signal $signal, restarting\n"; |
||
| 102 | $this->procsToStart++; |
||
| 103 | } |
||
| 104 | } elseif ( pcntl_wifexited( $status ) ) { |
||
| 105 | // Restart on non-zero exit status |
||
| 106 | $exitStatus = pcntl_wexitstatus( $status ); |
||
| 107 | if ( $exitStatus != 0 ) { |
||
| 108 | echo "Worker exited with status $exitStatus, restarting\n"; |
||
| 109 | $this->procsToStart++; |
||
| 110 | } else { |
||
| 111 | echo "Worker exited normally\n"; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | // Throttle restarts |
||
| 116 | if ( $this->procsToStart ) { |
||
| 117 | usleep( 500000 ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Run signal handlers |
||
| 122 | if ( function_exists( 'pcntl_signal_dispatch' ) ) { |
||
| 123 | pcntl_signal_dispatch(); |
||
| 124 | } else { |
||
| 125 | declare( ticks = 1 ) { |
||
| 126 | $status = $status; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | // Respond to TERM signal |
||
| 130 | if ( $this->termReceived ) { |
||
| 131 | foreach ( $this->children as $childPid => $unused ) { |
||
| 132 | posix_kill( $childPid, SIGTERM ); |
||
| 133 | } |
||
| 134 | $this->termReceived = false; |
||
| 135 | } |
||
| 136 | } while ( count( $this->children ) ); |
||
| 137 | pcntl_signal( SIGTERM, SIG_DFL ); |
||
| 138 | return 'done'; |
||
| 139 | } |
||
| 140 | |||
| 205 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.