| 1 | <?php |
||
| 6 | class Fork implements ForkInterface |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var Pipeline |
||
| 10 | */ |
||
| 11 | private $parent; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var array callable |
||
| 15 | */ |
||
| 16 | protected $forks = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var callable |
||
| 20 | */ |
||
| 21 | protected $resolver; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Fork constructor. |
||
| 25 | * |
||
| 26 | * @param Pipeline $pipeline |
||
| 27 | * @param callable|null $resolver |
||
| 28 | */ |
||
| 29 | public function __construct(Pipeline $pipeline, callable $resolver = null) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @inheritdoc |
||
| 37 | */ |
||
| 38 | public function join(callable $resolver = null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @inheritdoc |
||
| 52 | */ |
||
| 53 | public function disjoin($tag, callable $stage = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Chooses a fork or short-circuits based on $resolver |
||
| 68 | * |
||
| 69 | * @param mixed $payload |
||
| 70 | * @return mixed |
||
| 71 | */ |
||
| 72 | public function __invoke($payload) |
||
| 80 | } |
||
| 81 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.