Refactored \RTEX\Objects\Program\Instructions > Divide

This commit is contained in:
Netkas 2022-12-26 16:28:55 -05:00
parent b6ae2efc82
commit 7ff6dda8e0

View file

@ -8,8 +8,9 @@
use RTEX\Classes\InstructionBuilder; use RTEX\Classes\InstructionBuilder;
use RTEX\Classes\Utilities; use RTEX\Classes\Utilities;
use RTEX\Engine; use RTEX\Engine;
use RTEX\Exceptions\Core\MalformedInstructionException; use RTEX\Exceptions\EvaluationException;
use RTEX\Exceptions\Core\UnsupportedVariableType; use RTEX\Exceptions\InstructionException;
use RTEX\Exceptions\Runtime\ZeroDivisionException;
use RTEX\Interfaces\InstructionInterface; use RTEX\Interfaces\InstructionInterface;
class Divide implements InstructionInterface class Divide implements InstructionInterface
@ -38,7 +39,6 @@
* Returns an array representation of the instruction * Returns an array representation of the instruction
* *
* @return array * @return array
* @throws UnsupportedVariableType
*/ */
public function toArray(): array public function toArray(): array
{ {
@ -53,8 +53,7 @@
* *
* @param array $data * @param array $data
* @return InstructionInterface * @return InstructionInterface
* @throws MalformedInstructionException * @throws InstructionException
* @throws UnsupportedVariableType
*/ */
public static function fromArray(array $data): InstructionInterface public static function fromArray(array $data): InstructionInterface
{ {
@ -67,10 +66,14 @@
/** /**
* @param Engine $engine * @param Engine $engine
* @return int * @return int
* @throws UnsupportedVariableType * @throws ZeroDivisionException
* @throws EvaluationException
*/ */
public function eval(Engine $engine): int public function eval(Engine $engine): int
{ {
if ($this->B === 0)
throw new ZeroDivisionException(sprintf('Division by zero in %s', $this));
return (intval($engine->eval($this->A)) / intval($engine->eval($this->B))); return (intval($engine->eval($this->A)) / intval($engine->eval($this->B)));
} }
@ -78,7 +81,6 @@
* Returns the string representation of the instruction * Returns the string representation of the instruction
* *
* @return string * @return string
* @throws UnsupportedVariableType
*/ */
public function __toString(): string public function __toString(): string
{ {
@ -103,8 +105,7 @@
* Sets the value of A * Sets the value of A
* *
* @param mixed $A * @param mixed $A
* @throws UnsupportedVariableType * @throws InstructionException
* @throws MalformedInstructionException
*/ */
public function setA(mixed $A): void public function setA(mixed $A): void
{ {
@ -125,8 +126,7 @@
* Sets the value of B * Sets the value of B
* *
* @param mixed $B * @param mixed $B
* @throws MalformedInstructionException * @throws InstructionException
* @throws UnsupportedVariableType
*/ */
public function setB(mixed $B): void public function setB(mixed $B): void
{ {