Skip to content

Commit

Permalink
Merge 0cfd3a2 into 5108e39
Browse files Browse the repository at this point in the history
  • Loading branch information
iLexN authored May 3, 2021
2 parents 5108e39 + 0cfd3a2 commit 98f746e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 75 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: Execute tests
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml

- name: Upload coverage results to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:

- name: Benchmark
run: composer benchmark

- name: Infection check
env:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
}
],
"require": {
"php": ">=8.0"
"php": ">=8.0",
"ilexn/result-option": "0.1.1"
},
"require-dev": {
"codedungeon/phpunit-result-printer": "^0.30.1",
Expand Down
8 changes: 4 additions & 4 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand All @@ -10,10 +9,11 @@
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="tests" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
</issueHandlers>
<!-- <issueHandlers>-->
<!-- <LessSpecificReturnType errorLevel="info" />-->
<!-- </issueHandlers>-->
</psalm>
26 changes: 21 additions & 5 deletions src/HkIdValidResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

namespace Ilex\Validation\HkidValidation;

use Ilex\ResultOption\Option\Option;
use Ilex\Validation\HkidValidation\Reason\ReasonInterface;

final class HkIdValidResult implements \Stringable
{

/**
* HkIdValidResult constructor.
*
* @param \Ilex\ResultOption\Option\Option<\Ilex\Validation\HkidValidation\Hkid> $hkid
* @param \Ilex\Validation\HkidValidation\Reason\ReasonInterface $reason
*/
public function __construct(
private HkidValueInterface $hkid,
private Option $hkid,
private ReasonInterface $reason,
) {
}
Expand All @@ -37,33 +44,42 @@ public function getReason(): string

/**
* @return string
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function getPart1(): string
{
return $this->hkid->getPart1();
return $this->hkid->unwrap()->getPart1();
}

/**
* @return string
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function getPart2(): string
{
return $this->hkid->getPart2();
return $this->hkid->unwrap()->getPart2();
}

/**
* @return string
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function getPart3(): string
{
return $this->hkid->getPart3();
return $this->hkid->unwrap()->getPart3();
}

/**
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function format(): string
{
return $this->hkid->format();
return $this->hkid->unwrap()->format();
}

/**
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function __toString(): string
{
return $this->format();
Expand Down
29 changes: 18 additions & 11 deletions src/HkidDigitCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ilex\Validation\HkidValidation;

use Ilex\ResultOption\Option\Option;
use Ilex\Validation\HkidValidation\Reason\DigitError;
use Ilex\Validation\HkidValidation\Reason\Ok;
use Ilex\Validation\HkidValidation\Reason\PattenError;
Expand Down Expand Up @@ -131,6 +132,7 @@ private function clearString(string $string): string
* @param string $p3
*
* @return HkIdValidResult
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function checkParts(
string $p1,
Expand All @@ -148,17 +150,21 @@ public function checkParts(
* @param string $string
*
* @return HkIdValidResult
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function checkString(string $string): HkIdValidResult
{
$hkid = $this->validate($string);
$option = $this->validate($string);

if ($hkid instanceof HkidNull) {
return new HkIdValidResult($hkid, new PattenError());
if ($option->isNone()) {
return new HkIdValidResult($option, new PattenError());
}

$reason = $this->isValid($hkid) ? new Ok() : new DigitError();
return new HkIdValidResult($hkid, $reason);
if ($this->isValid($option->unwrap())) {
return new HkIdValidResult($option, new Ok()) ;
}

return new HkIdValidResult(Option::none(), new DigitError()) ;
}

private function isValid(HkidValueInterface $hkid): bool
Expand All @@ -174,18 +180,19 @@ private function isValid(HkidValueInterface $hkid): bool
*
* @param string $string
*
* @return HkidValueInterface
* @return \Ilex\ResultOption\Option\Option<\Ilex\Validation\HkidValidation\Hkid>
*/
private function validate(string $string): HkidValueInterface
private function validate(string $string): Option
{
if (1 === \preg_match(self::RE, $string, $matches)) {
return new Hkid(
return Option::some(new Hkid(
$this->clearString($matches['p1']),
$matches['p2'],
$this->clearString($matches['p3'])
);
));
}
return new HkidNull($string);

return Option::none();
}

/**
Expand Down Expand Up @@ -236,7 +243,7 @@ private function getPart2Remainder(string $p2, int $charSum): string
*/
private function calPart2Remainder(string $part2, int $charSum): int
{
$p2 = \array_map(fn (string $int): int => (int)$int, \str_split($part2));
$p2 = \array_map(static fn (string $int): int => (int)$int, \str_split($part2));

return self::MOD_NUM - ((
$charSum +
Expand Down
43 changes: 0 additions & 43 deletions src/HkidNull.php

This file was deleted.

16 changes: 7 additions & 9 deletions tests/HkidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@

class HkidTest extends TestCase
{

/**
* @dataProvider additionProviderTrueResult
*
* @param string $p1 CA
* @param string $p2 182361
* @param string $p3 1
* @param string $expectedFormat
*
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function testCheckPartsHkidFormatTrue(
string $p1,
Expand Down Expand Up @@ -47,6 +50,8 @@ public function testCheckPartsHkidFormatTrue(
* @param string $p2 182361
* @param string $p3 1
* @param string $expectedFormat
*
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function testCheckStringHkidFormatHelperTrue(
string $p1,
Expand Down Expand Up @@ -75,6 +80,8 @@ public function testCheckStringHkidFormatHelperTrue(
* @param string $p2 182361
* @param string $p3 1
* @param string $expectedFormat
*
* @throws \Ilex\ResultOption\Error\OptionException
*/
public function testCheckHkidFormatMainTrue(
string $p1,
Expand Down Expand Up @@ -138,36 +145,27 @@ public function additionProviderFalseResult(): Generator
* @param string $p2 182361
* @param string $p3 1
* @param \Ilex\Validation\HkidValidation\Reason\ReasonInterface $reason
* @param string $format
*/
public function testCheckHkidFormatFalse(
string $p1,
string $p2,
string $p3,
ReasonInterface $reason,
string $format
): void {
$a = Helper::checkByParts($p1, $p2, $p3);
self::assertFalse($a->isValid());
self::assertEquals($reason->getKey(), $a->getReason());
self::assertEquals($reason->isDigitError(), $a->isDigitError());
self::assertEquals($reason->isPattenError(), $a->isPattenError());
self::assertEquals($format, $a->format());

switch ($reason->getKey()) {
case ReasonInterface::DIGIT_ERROR:
self::assertFalse($a->isPattenError());
self::assertTrue($a->isDigitError());
self::assertEquals($p1, $a->getPart1());
self::assertEquals($p2, $a->getPart2());
self::assertEquals($p3, $a->getPart3());
break;
case ReasonInterface::PATTEN_ERROR:
self::assertTrue($a->isPattenError());
self::assertFalse($a->isDigitError());
self::assertEquals('', $a->getPart1());
self::assertEquals('', $a->getPart2());
self::assertEquals('', $a->getPart3());
break;
}

Expand Down

0 comments on commit 98f746e

Please sign in to comment.