diff --git a/src/Chances/Chance.php b/src/Chances/Chance.php index 0af2cc0..79cd8cd 100644 --- a/src/Chances/Chance.php +++ b/src/Chances/Chance.php @@ -1,7 +1,9 @@ percentage == 0) { return false; @@ -19,19 +24,12 @@ public function shouldRun() return $random <= $this->percentage; } - /** - * @return int - */ - public function getPercentage() + public function getPercentage(): int { return $this->percentage; } - /** - * @param int $percentage - * @return $this - */ - public function setPercentage($percentage) + public function setPercentage(int $percentage): self { $this->percentage = $percentage; diff --git a/src/Experiment.php b/src/Experiment.php index 741a1d1..aa0c0d6 100644 --- a/src/Experiment.php +++ b/src/Experiment.php @@ -1,7 +1,9 @@ name = $name; $this->laboratory = $laboratory; @@ -89,20 +88,16 @@ public function __construct($name, Laboratory $laboratory) /** * Fetch the experiment name. - * - * @return string */ - public function getName() + public function getName(): string { return $this->name; } /** * Retrieve the laboratory instance. - * - * @return \Scientist\Laboratory|null */ - public function getLaboratory() + public function getLaboratory(): ?Laboratory { return $this->laboratory; } @@ -110,12 +105,9 @@ public function getLaboratory() /** * Register a control callback. * - * @param callable $callback * @param mixed $context - * - * @return $this */ - public function control(callable $callback, $context = null) + public function control(callable $callback, $context = null): self { $this->control = $callback; $this->controlContext = $context; @@ -125,14 +117,15 @@ public function control(callable $callback, $context = null) /** * Fetch the control callback. - * - * @return callable */ - public function getControl() + public function getControl(): callable { return $this->control; } + /** + * @return mixed + */ public function getControlContext() { return $this->controlContext; @@ -141,12 +134,9 @@ public function getControlContext() /** * Register a trial callback. * - * @param string $name - * @param callable $callback - * - * @return $this + * @param mixed $context */ - public function trial($name, callable $callback, $context = null) + public function trial(string $name, callable $callback, $context = null): self { $this->trials[$name] = new Trial($name, $callback, $context); @@ -155,12 +145,8 @@ public function trial($name, callable $callback, $context = null) /** * Fetch a trial callback by name. - * - * @param string $name - * - * @return mixed */ - public function getTrial($name) + public function getTrial(string $name): callable { return $this->trials[$name]->getCallback(); } @@ -168,21 +154,17 @@ public function getTrial($name) /** * Fetch an array of trial callbacks. * - * @return array + * @return callable[] */ - public function getTrials() + public function getTrials(): array { return $this->trials; } /** * Set a matcher for this experiment. - * - * @param \Scientist\Matchers\Matcher $matcher - * - * @return $this */ - public function matcher(Matcher $matcher) + public function matcher(Matcher $matcher): self { $this->matcher = $matcher; @@ -191,22 +173,16 @@ public function matcher(Matcher $matcher) /** * Get the matcher for this experiment. - * - * @return \Scientist\Matchers\Matcher */ - public function getMatcher() + public function getMatcher(): Matcher { return $this->matcher; } /** * Set the execution chance. - * - * @param Chances\Chance $chance - * - * @return $this */ - public function chance(Chance $chance) + public function chance(Chance $chance): self { $this->chance = $chance; @@ -215,20 +191,16 @@ public function chance(Chance $chance) /** * Get the execution chance. - * - * @return Chances\Chance */ - public function getChance() + public function getChance(): Chance { return $this->chance; } /** * Determine whether an experiment should run based on chance. - * - * @return boolean */ - public function shouldRun() + public function shouldRun(): bool { return $this->chance ->shouldRun(); @@ -236,10 +208,8 @@ public function shouldRun() /** * Get the experiment parameters. - * - * @return array */ - public function getParams() + public function getParams(): array { return $this->params; } @@ -258,10 +228,8 @@ public function run() /** * Execute the experiment and return a report. - * - * @return \Scientist\Report */ - public function report() + public function report(): Report { $this->params = func_get_args(); diff --git a/src/Intern.php b/src/Intern.php index 44091b4..986367d 100644 --- a/src/Intern.php +++ b/src/Intern.php @@ -1,4 +1,5 @@ runControl($experiment); $trials = $this->runTrials($experiment); @@ -33,12 +30,8 @@ public function run(Experiment $experiment) /** * Run the control callback, and record its execution state. - * - * @param \Scientist\Experiment $experiment - * - * @return \Scientist\Result */ - protected function runControl(Experiment $experiment) + protected function runControl(Experiment $experiment): Result { return (new Machine( $experiment->getControl(), @@ -51,11 +44,9 @@ protected function runControl(Experiment $experiment) /** * Run trial callbacks and record their execution state. * - * @param \Scientist\Experiment $experiment - * - * @return \Scientist\Result[] + * @return Result[] */ - protected function runTrials(Experiment $experiment) + protected function runTrials(Experiment $experiment): array { $executions = []; @@ -74,11 +65,9 @@ protected function runTrials(Experiment $experiment) /** * Determine whether trial results match the control. * - * @param \Scientist\Matchers\Matcher $matcher - * @param \Scientist\Result $control - * @param \Scientist\Result[] $trials + * @param Result[] $trials */ - protected function determineMatches(Matcher $matcher, Result $control, array $trials = []) + protected function determineMatches(Matcher $matcher, Result $control, array $trials = []): void { foreach ($trials as $trial) { if ($matcher->match($control->getValue(), $trial->getValue())) { diff --git a/src/Journals/Journal.php b/src/Journals/Journal.php index 7b8151f..87b7299 100644 --- a/src/Journals/Journal.php +++ b/src/Journals/Journal.php @@ -1,4 +1,5 @@ experiment = $experiment; $this->report = $report; @@ -42,20 +38,16 @@ public function report(Experiment $experiment, Report $report) /** * Get the experiment. - * - * @return \Scientist\Experiment */ - public function getExperiment() + public function getExperiment(): Experiment { return $this->experiment; } /** * Get the experiment report. - * - * @return \Scientist\Report */ - public function getReport() + public function getReport(): Report { return $this->report; } diff --git a/src/Laboratory.php b/src/Laboratory.php index 489ad28..f32a2dd 100644 --- a/src/Laboratory.php +++ b/src/Laboratory.php @@ -1,4 +1,5 @@ journals = []; foreach ($journals as $journal) { @@ -40,12 +39,8 @@ public function setJournals(array $journals = []) /** * Register a new journal. - * - * @param \Scientist\Journals\Journal $journal - * - * @return $this */ - public function addJournal(Journal $journal) + public function addJournal(Journal $journal): self { $this->journals[] = $journal; @@ -55,9 +50,9 @@ public function addJournal(Journal $journal) /** * Retrieve registers journals. * - * @return array + * @return Journal[] */ - public function getJournals() + public function getJournals(): array { return $this->journals; } @@ -65,11 +60,9 @@ public function getJournals() /** * Start a new experiment. * - * @param string $name - * * @return mixed */ - public function experiment($name) + public function experiment(string $name) { return (new Experiment($name, $this)); } @@ -77,8 +70,6 @@ public function experiment($name) /** * Run an experiment. * - * @param \Scientist\Experiment $experiment - * * @return mixed */ public function runExperiment(Experiment $experiment) @@ -96,12 +87,8 @@ public function runExperiment(Experiment $experiment) /** * Run an experiment and return the result. - * - * @param \Scientist\Experiment $experiment - * - * @return \Scientist\Report */ - public function getReport(Experiment $experiment) + public function getReport(Experiment $experiment): Report { $report = (new Intern)->run($experiment); $this->reportToJournals($experiment, $report); @@ -111,13 +98,8 @@ public function getReport(Experiment $experiment) /** * Report experiment result to registered journals. - * - * @param \Scientist\Experiment $experiment - * @param \Scientist\Report $report - * - * @return void */ - protected function reportToJournals(Experiment $experiment, Report $report) + protected function reportToJournals(Experiment $experiment, Report $report): void { foreach ($this->journals as $journal) { $journal->report($experiment, $report); diff --git a/src/Machine.php b/src/Machine.php index 8277b3a..1760c28 100644 --- a/src/Machine.php +++ b/src/Machine.php @@ -1,4 +1,5 @@ callback = $callback; $this->params = $params; @@ -56,10 +55,8 @@ public function __construct(callable $callback, array $params = [], $muted = fal /** * Execute the callback and retrieve a result. - * - * @return \Scientist\Result */ - public function execute() + public function execute(): Result { $this->setStartValues(); $this->executeCallback(); @@ -70,10 +67,8 @@ public function execute() /** * Set values before callback is executed. - * - * @return void */ - protected function setStartValues() + protected function setStartValues(): void { $this->result->setStartTime(microtime(true)); $this->result->setStartMemory(memory_get_usage()); @@ -81,13 +76,12 @@ protected function setStartValues() /** * Execute the callback with parameters. - * - * @return void */ - protected function executeCallback() + protected function executeCallback(): void { if ($this->muted) { - return $this->executeMutedCallback(); + $this->executeMutedCallback(); + return; } $this->result->setValue(call_user_func_array($this->callback, $this->params)); @@ -95,14 +89,12 @@ protected function executeCallback() /** * Execute the callback, but swallow exceptions. - * - * @return void */ - protected function executeMutedCallback() + protected function executeMutedCallback(): void { try { $this->result->setValue(call_user_func_array($this->callback, $this->params)); - } catch (Throwable $exception) { + } catch (\Throwable $exception) { $this->result->setException($exception); $this->result->setValue(null); } @@ -110,10 +102,8 @@ protected function executeMutedCallback() /** * Set values after the callback has executed. - * - * @return void */ - protected function setEndValues() + protected function setEndValues(): void { $this->result->setEndTime(microtime(true)); $this->result->setEndMemory(memory_get_usage()); diff --git a/src/Matchers/ClosureMatcher.php b/src/Matchers/ClosureMatcher.php index 9069f73..37fe5c3 100644 --- a/src/Matchers/ClosureMatcher.php +++ b/src/Matchers/ClosureMatcher.php @@ -1,4 +1,5 @@ closure, $control, $trial); } diff --git a/src/Matchers/Matcher.php b/src/Matchers/Matcher.php index 7e8fb3a..45dbeec 100644 --- a/src/Matchers/Matcher.php +++ b/src/Matchers/Matcher.php @@ -1,4 +1,5 @@ name = $name; $this->control = $control; @@ -49,32 +46,24 @@ public function __construct($name, Result $control, array $trials = []) /** * Get the experiment name. - * - * @return string */ - public function getName() + public function getName(): string { return $this->name; } /** * Get the control result instance. - * - * @return \Scientist\Result */ - public function getControl() + public function getControl(): Result { return $this->control; } /** * Get a trial result instance by name. - * - * @param string $name - * - * @return \Scientist\Result */ - public function getTrial($name) + public function getTrial(string $name): Result { return $this->trials[$name]; } @@ -82,9 +71,9 @@ public function getTrial($name) /** * Get the trial result instances. * - * @return array + * @return Result[] */ - public function getTrials() + public function getTrials(): array { return $this->trials; } diff --git a/src/Result.php b/src/Result.php index 6e78329..c961ee7 100644 --- a/src/Result.php +++ b/src/Result.php @@ -1,4 +1,6 @@ context = $context; @@ -86,10 +91,8 @@ public function getValue() * Set the callback result value. * * @param mixed $value - * - * @return $this */ - public function setValue($value) + public function setValue($value): self { $this->value = $value; @@ -98,22 +101,16 @@ public function setValue($value) /** * Get the callback execution start time. - * - * @return float */ - public function getStartTime() + public function getStartTime(): float { return $this->startTime; } /** * Set the callback execution start time. - * - * @param float $startTime - * - * @return $this */ - public function setStartTime($startTime) + public function setStartTime(float $startTime): self { $this->startTime = $startTime; @@ -122,22 +119,16 @@ public function setStartTime($startTime) /** * Get the callback execution end time. - * - * @return float */ - public function getEndTime() + public function getEndTime(): float { return $this->endTime; } /** * Set the callback execution end time. - * - * @param float $endTime - * - * @return $this */ - public function setEndTime($endTime) + public function setEndTime(float $endTime): self { $this->endTime = $endTime; @@ -146,32 +137,24 @@ public function setEndTime($endTime) /** * Get the execution time of the callback. - * - * @return float */ - public function getTime() + public function getTime(): float { return $this->endTime - $this->startTime; } /** * Get the callback execution starting memory usage. - * - * @return float */ - public function getStartMemory() + public function getStartMemory(): float { return $this->startMemory; } /** * Set the callback execution starting memory usage. - * - * @param float $startMemory - * - * @return $this */ - public function setStartMemory($startMemory) + public function setStartMemory(float $startMemory): self { $this->startMemory = $startMemory; @@ -180,22 +163,16 @@ public function setStartMemory($startMemory) /** * Get the callback execution ending memory usage. - * - * @return float */ - public function getEndMemory() + public function getEndMemory(): float { return $this->endMemory; } /** * Set the callback execution ending memory usage. - * - * @param float $endMemory - * - * @return $this */ - public function setEndMemory($endMemory) + public function setEndMemory(float $endMemory): self { $this->endMemory = $endMemory; @@ -204,38 +181,33 @@ public function setEndMemory($endMemory) /** * Get the memory spike amount of the callback. - * - * @return float */ - public function getMemory() + public function getMemory(): float { return $this->endMemory - $this->startMemory; } /** * Get the exception thrown by the callback. - * - * @return Exception|null */ - public function getException() + public function getException(): ?\Throwable { return $this->exception; } /** * Set the exception thrown by the callback. - * - * @param Exception|null $exception - * - * @return $this */ - public function setException($exception) + public function setException(?\Throwable $exception): self { $this->exception = $exception; return $this; } + /** + * @return mixed + */ public function getContext() { return $this->context; @@ -243,22 +215,16 @@ public function getContext() /** * Determine whether the callback result matches the control. - * - * @return boolean */ - public function isMatch() + public function isMatch(): bool { return $this->match; } /** * Set whether the callback result matches the control. - * - * @param boolean $match - * - * @return $this */ - public function setMatch($match) + public function setMatch(bool $match): self { $this->match = $match; diff --git a/src/Trial.php b/src/Trial.php index c553687..685a96b 100644 --- a/src/Trial.php +++ b/src/Trial.php @@ -1,4 +1,5 @@ name = $name; $this->callback = $callback; $this->context = $context; } - public function getName() + public function getName(): string { return $this->name; } - public function getCallback() + public function getCallback(): callable { return $this->callback; } + /** + * @return mixed + */ public function getContext() { return $this->context; diff --git a/tests/Chances/StandardChanceTest.php b/tests/Chances/StandardChanceTest.php index 9653134..35eb30a 100644 --- a/tests/Chances/StandardChanceTest.php +++ b/tests/Chances/StandardChanceTest.php @@ -1,4 +1,6 @@ assertIsFloat($r->getTrial('trial')->getStartTime()); $this->assertIsFloat($r->getTrial('trial')->getEndTime()); $this->assertIsFloat($r->getTrial('trial')->getTime()); - $this->assertIsInt($r->getControl()->getStartMemory()); - $this->assertIsInt($r->getControl()->getEndMemory()); - $this->assertIsInt($r->getControl()->getMemory()); - $this->assertIsInt($r->getTrial('trial')->getStartMemory()); - $this->assertIsInt($r->getTrial('trial')->getEndMemory()); - $this->assertIsInt($r->getTrial('trial')->getMemory()); + $this->assertIsFloat($r->getControl()->getStartMemory()); + $this->assertIsFloat($r->getControl()->getEndMemory()); + $this->assertIsFloat($r->getControl()->getMemory()); + $this->assertIsFloat($r->getTrial('trial')->getStartMemory()); + $this->assertIsFloat($r->getTrial('trial')->getEndMemory()); + $this->assertIsFloat($r->getTrial('trial')->getMemory()); $this->assertNull($r->getControl()->getException()); $this->assertNull($r->getTrial('trial')->getException()); $this->assertFalse($r->getTrial('trial')->isMatch()); diff --git a/tests/MachineTest.php b/tests/MachineTest.php index 30841d6..f913860 100644 --- a/tests/MachineTest.php +++ b/tests/MachineTest.php @@ -1,4 +1,5 @@ execute(); - $this->assertIsInt($r->getStartMemory()); - $this->assertIsInt($r->getEndMemory()); + $this->assertIsFloat($r->getStartMemory()); + $this->assertIsFloat($r->getEndMemory()); } } diff --git a/tests/Matchers/ClosureMatcherTest.php b/tests/Matchers/ClosureMatcherTest.php index 61641b2..ccb07ba 100644 --- a/tests/Matchers/ClosureMatcherTest.php +++ b/tests/Matchers/ClosureMatcherTest.php @@ -1,4 +1,5 @@ setMatch(true); - $this->assertTrue(true, $r->isMatch()); + $this->assertTrue($r->isMatch()); } public function test_can_have_context()