Skip to content

Commit

Permalink
Merge pull request #127 from smartemailing/m_int_zero_decimals
Browse files Browse the repository at this point in the history
Floats that contain only zero floating part can now be extracted as Ints
  • Loading branch information
mstrouhal committed Aug 5, 2023
2 parents cb74a7c + 86898a1 commit 525acbd
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 75 deletions.
6 changes: 1 addition & 5 deletions src/Base64String.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ final class Base64String implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private string $value;

private function __construct(
string $value
private string $value
)
{
if (!$this->isValid($value)) {
throw new InvalidTypeException('Invalid Base64 string');
}

$this->value = $value;
}

public static function encode(
Expand Down
6 changes: 1 addition & 5 deletions src/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class Guid implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private string $value;

private function __construct(
string $value
private string $value
) {
if (!\preg_match('/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $value)) {
throw new InvalidTypeException('Invalid guid value');
}

$this->value = $value;
}

public static function fromHex32(
Expand Down
6 changes: 1 addition & 5 deletions src/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ final class Iban implements ToStringInterface, ComparableInterface

private \Iban\Validation\Iban $iban;

private string $value;

public function __construct(
string $value
private string $value
)
{
if (!\class_exists(\Iban\Validation\Iban::class) || !\class_exists(\Iban\Validation\Validator::class)) {
Expand All @@ -36,8 +34,6 @@ public function __construct(
if (!$validator->validate($this->iban)) {
throw new InvalidTypeException('Invalid Iban: ' . $value);
}

$this->value = $value;
}

public function getValue(): string
Expand Down
4 changes: 4 additions & 0 deletions src/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final public static function from(
return (int) $value;
}

if (Validators::isNumeric($value) && (int) $value === (int) \ceil(\abs((float) $value))) {
return (int) $value;
}

throw InvalidTypeException::typeError('int', $value);
}

Expand Down
6 changes: 1 addition & 5 deletions src/JsonString.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ final class JsonString implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private string $value;

private function __construct(
string $value
private string $value
)
{
if (!$this->isValid($value)) {
throw new InvalidTypeException('Invalid JSON string');
}

$this->value = $value;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class Part implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private float $value;

public function __construct(
float $value
private float $value
) {
if ($value < 0 || $value > 1) {
throw new InvalidTypeException('Invalid part: ' . $value);
}

$this->value = $value;
}

public static function fromRatio(
Expand Down
6 changes: 1 addition & 5 deletions src/Port.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class Port implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private int $value;

public function __construct(
int $value
private int $value
) {
if ($value < 0 || $value > 65535) {
throw new InvalidTypeException('Invalid Port number: ' . $value);
}

$this->value = $value;
}

public function getValue(): int
Expand Down
6 changes: 1 addition & 5 deletions src/Quantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class Quantity implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private int $value;

public function __construct(
int $value
private int $value
) {
if ($value < 1 || $value > \PHP_INT_MAX) {
throw new InvalidTypeException('Invalid quantity: ' . $value);
}

$this->value = $value;
}

public function getValue(): int
Expand Down
6 changes: 1 addition & 5 deletions src/ReLUValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class ReLUValue implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private float $value;

public function __construct(
float $value
private float $value
) {
if ($value < 0.0) {
throw new InvalidTypeException('Invalid ReLU value: ' . $value);
}

$this->value = $value;
}

public function getValue(): float
Expand Down
9 changes: 1 addition & 8 deletions src/ScalarLeavesArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ final class ScalarLeavesArray implements ToArrayInterface, ComparableInterface
use ArrayExtractableTrait;
use ArrayComparableTrait;

/**
* @var array<mixed>
*/
private array $data;

/**
* @param array<mixed> $data
*/
public function __construct(
array $data
private array $data
) {
if (!ValidationHelpers::isScalarLeavesArray($data)) {
throw new InvalidTypeException('Array must have all it\'s leaves scalar or null');
}

$this->data = $data;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/SigmoidValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class SigmoidValue implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private float $value;

public function __construct(
float $value
private float $value
) {
if ($value < -1 || $value > 1) {
throw new InvalidTypeException('Invalid sigmoid value: ' . $value);
}

$this->value = $value;
}

public function getValue(): float
Expand Down
6 changes: 1 addition & 5 deletions src/SwiftBic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ final class SwiftBic implements ToStringInterface, ComparableInterface
use StringExtractableTrait;
use StringComparableTrait;

private string $value;

public function __construct(
string $value
private string $value
)
{
$this->value = $value;

if (!$this->isValid($this->value)) {
throw new InvalidTypeException('Invalid Swift/Bic: ' . $value);
}
Expand Down
6 changes: 1 addition & 5 deletions src/UnsignedFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class UnsignedFloat implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private float $value;

public function __construct(
float $value
private float $value
) {
if ($value < 0.0 || $value > \PHP_INT_MAX) {
throw new InvalidTypeException('Invalid unsigned float: ' . $value);
}

$this->value = $value;
}

public function getValue(): float
Expand Down
6 changes: 1 addition & 5 deletions src/UnsignedInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ final class UnsignedInt implements ToStringInterface, ComparableInterface
use ToStringTrait;
use StringComparableTrait;

private int $value;

public function __construct(
int $value
private int $value
) {
if ($value < 0 || $value > \PHP_INT_MAX) {
throw new InvalidTypeException('Invalid unsigned integer: ' . $value);
}

$this->value = $value;
}

public function getValue(): int
Expand Down
7 changes: 4 additions & 3 deletions tests/IntArrayUniqueTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class IntArrayUniqueTest extends TestCase
new \stdClass(),
],
[
1.0,
1.01,
2,
3,
],
Expand All @@ -40,6 +40,7 @@ final class IntArrayUniqueTest extends TestCase
foreach ($invalidValues as $invalidValue) {
Assert::throws(
static function () use ($invalidValue): void {
echo 'Testing ' . \var_export($invalidValue, true) . \PHP_EOL;
UniqueIntArray::from($invalidValue);
},
InvalidTypeException::class
Expand All @@ -49,12 +50,12 @@ final class IntArrayUniqueTest extends TestCase
$validValues = [

[
'1',
'1.00',
'2',
'3',
],
[
1,
1.0,
2,
3,
],
Expand Down
60 changes: 60 additions & 0 deletions tests/IntTypeTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types = 1);

namespace SmartEmailing\Types;

use Tester\Assert;
use Tester\TestCase;

require __DIR__ . '/bootstrap.php';

final class IntTypeTest extends TestCase
{

public function testInvalid(): void
{
$invalid = [
true,
false,
null,
new class {

},
[],
'x',
1.001,
'1.001',
];

foreach ($invalid as $value) {
Assert::throws(
static function () use ($value) {
echo 'Trying invalid: ' . \var_export($value, true) . \PHP_EOL;
IntType::from($value);
},
InvalidTypeException::class
);
}
}

public function testValid(): void
{
$invalid = [
1,
'1',
1.00000,
'1.00000',
];

foreach ($invalid as $value) {
echo 'Trying valid: ' . \var_export($value, true) . \PHP_EOL;
Assert::true(
\is_int(IntType::from($value))
);
}
}

}

(new IntTypeTest())->run();
1 change: 1 addition & 0 deletions tests/UnsignedIntTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class UnsignedIntTest extends TestCase
foreach ($invalidValues as $validValue) {
Assert::throws(
static function () use ($validValue): void {
echo 'Testing ' . \var_export($validValue, true) . \PHP_EOL;
UnsignedInt::from($validValue);
},
InvalidTypeException::class
Expand Down
6 changes: 2 additions & 4 deletions tools/cs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh"/>
<exclude name="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion"/>
<exclude name="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
<exclude name="SlevomatCodingStandard.Classes.ForbiddenPublicProperty"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion.DisallowedConstructorPropertyPromotion"/>
<exclude name="SlevomatCodingStandard.Classes.ForbiddenPublicProperty.ForbiddenPublicProperty"/>

<!-- cannot be disabled for methods implementing interface -->
<exclude name="SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter"/>
Expand Down Expand Up @@ -81,7 +81,6 @@
<rule ref="SlevomatCodingStandard.Functions.RequireMultiLineCall">
<properties>
<property name="minLineLength" type="int" value="200"/>
<property name="ignoreWithComplexParameter" type="boolean" value="true"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
Expand All @@ -101,7 +100,6 @@
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="newlinesCountBetweenOpenTagAndDeclare" value="2"/>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
Expand Down

0 comments on commit 525acbd

Please sign in to comment.