Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"config": {
"platform": {
"php": "7.4"
"php": "8.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Nope, TalentLMS is still on 7.4

}
},
"require-dev": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
phpVersion="7.4"
phpVersion="8.0"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
12 changes: 9 additions & 3 deletions src/Number/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class Integer extends AbstractType
{
private int $value;

public function __construct(int $value)
final public function __construct(int $value)
{
$this->validate($value);
$this->value = $value;
}

Expand All @@ -25,12 +26,17 @@ final public function getValue(): int
* @param int|float|string $value
* @return static
*/
final public static function fromNumeric($value): self
final public static function fromNumeric($value): static
{
if (is_numeric($value)) {
return new self((int)$value);
return new static((int)$value);
}

throw new InvalidArgumentException('Value is not numeric.');
}

protected function validate(int $value): void
{
// Integer does nothing.
}
}
4 changes: 1 addition & 3 deletions src/Number/NonNegativeInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

class NonNegativeInteger extends Integer
{
public function __construct(int $value)
protected function validate(int $value): void
{
if ($value < 0) {
throw new InvalidArgumentException('Value must be a non negative integer');
}

parent::__construct($value);
}
}
4 changes: 1 addition & 3 deletions src/Number/PositiveInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

class PositiveInteger extends Integer
{
public function __construct(int $value)
protected function validate(int $value): void
{
if ($value <= 0) {
throw new InvalidArgumentException('Value must be a positive integer');
}

parent::__construct($value);
}
}
21 changes: 21 additions & 0 deletions tests/Number/NonNegativeIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,25 @@ public function test_isNotEqualToDifferentType(): void
$this->assertFalse($integer1->equals($integer2));
$this->assertFalse($integer2->equals($integer1));
}

public function test_canNotBeCreatedFromNegativeNumericString(): void
{
$this->expectException(InvalidArgumentException::class);

NonNegativeInteger::fromNumeric('-42');
}

public function test_canNotBeCreatedFromNegativeFloat(): void
{
$this->expectException(InvalidArgumentException::class);

$result = NonNegativeInteger::fromNumeric(-42.3);
}

public function test_canNotBeCreatedFromNegativeNumeric(): void
{
$this->expectException(InvalidArgumentException::class);

NonNegativeInteger::fromNumeric(-42);
}
}
28 changes: 28 additions & 0 deletions tests/Number/PositiveIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,32 @@ public function test_isNotEqualToDifferentType(): void
$this->assertFalse($integer1->equals($integer2));
$this->assertFalse($integer2->equals($integer1));
}

public function test_CannotBeCreatedFromNumericZeroString(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric('0');
}

public function test_CannotBeCreatedFromNumericZeroFloat(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric(0.0);
}

public function test_CannotBeCreatedFromNumericNegativeIntegerString(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric('-1');
}

public function test_CannotBeCreatedFromNumericNegativeInteger(): void
{
$this->expectException(InvalidArgumentException::class);

PositiveInteger::fromNumeric(-1);
}
}
Loading