| Conditions | 3 |
| Paths | 1 |
| Total Lines | 73 |
| 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 |
||
| 60 | public function configure(ExerciseDispatcher $exerciseDispatcher) |
||
| 61 | { |
||
| 62 | $eventDispatcher = $exerciseDispatcher->getEventDispatcher(); |
||
| 63 | |||
| 64 | $appendArgsListener = function (CliExecuteEvent $event) { |
||
| 65 | $event->appendArg('127.0.0.1'); |
||
| 66 | $event->appendArg($this->getRandomPort()); |
||
| 67 | }; |
||
| 68 | |||
| 69 | $eventDispatcher->listen('cli.verify.reference-execute.pre', $appendArgsListener); |
||
| 70 | $eventDispatcher->listen('cli.verify.student-execute.pre', $appendArgsListener); |
||
| 71 | $eventDispatcher->listen('cli.run.student-execute.pre', $appendArgsListener); |
||
| 72 | |||
| 73 | $eventDispatcher->listen('cli.verify.reference.executing', function (CliExecuteEvent $event) { |
||
| 74 | $args = $event->getArgs()->getArrayCopy(); |
||
| 75 | $client = $this->socketFactory->createClient(...$args); |
||
| 76 | |||
| 77 | //wait for server to boot |
||
| 78 | usleep(100000); |
||
| 79 | |||
| 80 | $client->connect(); |
||
| 81 | $client->readAll(); |
||
| 82 | |||
| 83 | //wait for shutdown |
||
| 84 | usleep(100000); |
||
| 85 | }); |
||
| 86 | |||
| 87 | $eventDispatcher->insertVerifier('cli.verify.student.executing', function (CliExecuteEvent $event) { |
||
| 88 | $args = $event->getArgs()->getArrayCopy(); |
||
| 89 | $client = $this->socketFactory->createClient(...$args); |
||
| 90 | |||
| 91 | //wait for server to boot |
||
| 92 | usleep(100000); |
||
| 93 | |||
| 94 | try { |
||
| 95 | $client->connect(); |
||
| 96 | } catch (Exception $e) { |
||
| 97 | return Failure::fromNameAndReason($this->getName(), $e->getMessage()); |
||
| 98 | } |
||
| 99 | |||
| 100 | $out = $client->readAll(); |
||
| 101 | |||
| 102 | //wait for shutdown |
||
| 103 | usleep(100000); |
||
| 104 | |||
| 105 | $date = new \DateTime; |
||
| 106 | |||
| 107 | //match the current date but any seconds |
||
| 108 | //since we can't mock time in PHP easily |
||
| 109 | if (!preg_match(sprintf('/^%s:([0-5][0-9]|60)\n$/', $date->format('Y-m-d H:i')), $out)) { |
||
| 110 | return ComparisonFailure::fromNameAndValues($this->getName(), $date->format("Y-m-d H:i:s\n"), $out); |
||
| 111 | } |
||
| 112 | return new Success($this->getName()); |
||
| 113 | }); |
||
| 114 | |||
| 115 | $eventDispatcher->listen('cli.run.student.executing', function (CliExecuteEvent $event) { |
||
| 116 | /** @var OutputInterface $output */ |
||
| 117 | $output = $event->getParameter('output'); |
||
| 118 | $args = $event->getArgs()->getArrayCopy(); |
||
| 119 | $client = $this->socketFactory->createClient(...$args); |
||
| 120 | |||
| 121 | //wait for server to boot |
||
| 122 | usleep(100000); |
||
| 123 | |||
| 124 | $client->connect(); |
||
| 125 | $out = $client->readAll(); |
||
| 126 | |||
| 127 | //wait for shutdown |
||
| 128 | usleep(100000); |
||
| 129 | |||
| 130 | $output->write($out); |
||
| 131 | }); |
||
| 132 | } |
||
| 133 | |||
| 158 |
This check looks for function calls that miss required arguments.