Skip to content

Commit

Permalink
Refactor API and Cleanup Generics (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Jul 6, 2023
2 parents ee40b6f + 5991c8a commit e922fe4
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 402 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"autoload-dev": {
"psr-4": {
"Ghostwriter\\Result\\Tests\\": "tests/"
"Ghostwriter\\Result\\Tests\\Unit\\": "tests/Unit"
}
},
"config": {
Expand Down Expand Up @@ -88,7 +88,7 @@
"missing-returntypes": "psalm --alter --issues=MissingReturnType",
"normalizer": "composer normalize --no-check-lock",
"phpbench": "phpbench run",
"phpunit": "phpunit --colors=always --testdox --stop-on-failure",
"phpunit": "phpunit --colors=always",
"psalm": "@psalm:shepherd",
"psalm:baseline": "psalm --set-baseline=psalm-baseline.xml",
"psalm:dry-run": "psalm --alter --issues=all --dry-run",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

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

8 changes: 0 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
failOnSkipped="true"
failOnWarning="true"
requireCoverageMetadata="true"
stopOnDefect="true"
stopOnError="true"
stopOnFailure="true"
stopOnIncomplete="true"
stopOnRisky="true"
stopOnSkipped="true"
stopOnWarning="true"
testdox="true"
>
<coverage>
<report>
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.11.0@c9b192ab8400fdaf04b2b13d110575adc879aa90"/>
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015"/>
112 changes: 4 additions & 108 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,13 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector;
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
use Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector;
use Rector\PHPUnit\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
use Rector\PHPUnit\Rector\Class_\RemoveDataProviderTestPrefixRector;
use Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector;
use Rector\PHPUnit\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
use Rector\PHPUnit\Rector\ClassMethod\ExceptionAnnotationRector;
use Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector;
use Rector\PHPUnit\Rector\ClassMethod\TryCatchToExpectExceptionRector;
use Rector\PHPUnit\Rector\Foreach_\SimplifyForeachInstanceOfRector;
use Rector\PHPUnit\Rector\MethodCall\AssertCompareToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\AssertComparisonToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\AssertEqualsParameterToSpecificMethodsTypeRector;
use Rector\PHPUnit\Rector\MethodCall\AssertEqualsToSameRector;
use Rector\PHPUnit\Rector\MethodCall\AssertFalseStrposToContainsRector;
use Rector\PHPUnit\Rector\MethodCall\AssertInstanceOfComparisonRector;
use Rector\PHPUnit\Rector\MethodCall\AssertIssetToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\AssertNotOperatorRector;
use Rector\PHPUnit\Rector\MethodCall\AssertPropertyExistsRector;
use Rector\PHPUnit\Rector\MethodCall\AssertRegExpRector;
use Rector\PHPUnit\Rector\MethodCall\AssertResourceToClosedResourceRector;
use Rector\PHPUnit\Rector\MethodCall\AssertSameBoolNullToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\AssertSameTrueFalseToAssertTrueFalseRector;
use Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseInternalTypeToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector;
use Rector\PHPUnit\Rector\MethodCall\CreateMockToCreateStubRector;
use Rector\PHPUnit\Rector\MethodCall\DelegateExceptionArgumentsRector;
use Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector;
use Rector\PHPUnit\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
use Rector\PHPUnit\Rector\MethodCall\RemoveExpectAnyFromMockRector;
use Rector\PHPUnit\Rector\MethodCall\ReplaceAssertArraySubsetWithDmsPolyfillRector;
use Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector;
use Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector;
use Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector;
use Rector\PHPUnit\Rector\MethodCall\UseSpecificWillMethodRector;
use Rector\PHPUnit\Rector\StaticCall\GetMockRector;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->importShortClasses();
$rectorConfig->parallel();
$rectorConfig->sets([
PHPUnitLevelSetList::UP_TO_PHPUNIT_90,
// PHPUnitLevelSetList::UP_TO_PHPUNIT_100,
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::NAMING,
SetList::PRIVATIZATION,
SetList::PSR_4,
SetList::TYPE_DECLARATION,
SetList::EARLY_RETURN,
SetList::PHP_81,
]);
$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']);
$rectorConfig->import(__DIR__ . '/vendor/ghostwriter/coding-standard/rector.php');
$rectorConfig->phpVersion(PhpVersion::PHP_81);
$rectorConfig->skip([
__DIR__ . '*/tests/Fixture/*',
__DIR__ . '*/vendor/*',
AddDoesNotPerformAssertionToNonAssertingTestRector::class,
CallableThisArrayToAnonymousFunctionRector::class,
PseudoNamespaceToNamespaceRector::class,
RemoveEmptyClassMethodRector::class,
RemoveEmptyTestMethodRector::class,
StringClassNameToClassConstantRector::class,
]);
$rectorConfig->rules([
AddSeeTestAnnotationRector::class,
AssertCompareToSpecificMethodRector::class,
AssertComparisonToSpecificMethodRector::class,
AssertEqualsParameterToSpecificMethodsTypeRector::class,
AssertEqualsToSameRector::class,
AssertFalseStrposToContainsRector::class,
AssertInstanceOfComparisonRector::class,
AssertIssetToSpecificMethodRector::class,
AssertNotOperatorRector::class,
AssertPropertyExistsRector::class,
AssertRegExpRector::class,
AssertResourceToClosedResourceRector::class,
AssertSameBoolNullToSpecificMethodRector::class,
AssertSameTrueFalseToAssertTrueFalseRector::class,
AssertTrueFalseInternalTypeToSpecificMethodRector::class,
AssertTrueFalseToSpecificMethodRector::class,
ConstructClassMethodToSetUpTestCaseRector::class,
CreateMockToCreateStubRector::class,
DelegateExceptionArgumentsRector::class,
ExceptionAnnotationRector::class,
ExplicitPhpErrorApiRector::class,
GetMockBuilderGetMockToCreateMockRector::class,
GetMockRector::class,
RemoveDataProviderTestPrefixRector::class,
RemoveEmptyTestMethodRector::class,
RemoveExpectAnyFromMockRector::class,
ReplaceAssertArraySubsetWithDmsPolyfillRector::class,
RestoreDefaultNullToNullableTypePropertyRector::class,
SimplifyForeachInstanceOfRector::class,
SpecificAssertContainsRector::class,
SpecificAssertContainsWithoutIdentityRector::class,
SpecificAssertInternalTypeRector::class,
TestListenerToHooksRector::class,
TryCatchToExpectExceptionRector::class,
UseSpecificWillMethodRector::class,
]);

$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']);
$rectorConfig->skip([__DIR__ . '*/tests/Fixture/*', __DIR__ . '*/vendor/*']);
};
29 changes: 20 additions & 9 deletions src/AbstractResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Ghostwriter\Option\None;
use Ghostwriter\Option\Option;
use Ghostwriter\Option\OptionInterface;
use Ghostwriter\Result\Contract\ErrorInterface;
use Ghostwriter\Result\Contract\ResultInterface;
use Ghostwriter\Result\Contract\SuccessInterface;
use Ghostwriter\Result\Exception\ResultException;
use Throwable;

Expand All @@ -22,8 +19,10 @@ abstract class AbstractResult implements ResultInterface
{
/**
* @var OptionInterface<TValue>
*
* @readonly
*/
private readonly OptionInterface $option;
private OptionInterface $option;

/**
* @param TValue $value
Expand Down Expand Up @@ -75,8 +74,10 @@ public function expectError(Throwable $throwable): Throwable
throw $throwable;
}

/** @var OptionInterface<Throwable> $this->option */
return $this->option->unwrap();
/** @var Throwable $throwable */
$throwable = $this->option->unwrap();

return $throwable;
}

public function isError(): bool
Expand Down Expand Up @@ -149,7 +150,7 @@ public function unwrap(): mixed
return $this->option->unwrap();
}

throw ResultException::invalidMethodCall('unwrap', ErrorInterface::class);
throw new ResultException(sprintf('Invalid method call "unwrap()" on a Result of type %s', static::class));
}

public function unwrapError(): mixed
Expand All @@ -158,7 +159,9 @@ public function unwrapError(): mixed
return $this->option->unwrap();
}

throw ResultException::invalidMethodCall('unwrapError', SuccessInterface::class);
throw new ResultException(
sprintf('Invalid method call "unwrapError()" on a Result of type %s', static::class)
);
}

public function unwrapOr(mixed $fallback): mixed
Expand All @@ -180,10 +183,18 @@ public function unwrapOrElse(callable $function): mixed
->unwrap();
}

/**
* @template TNewValue
*
* @param callable(TValue):TNewValue $function
*/
private function call(callable $function): ResultInterface
{
try {
return self::of($function($this->option->unwrap()));
/** @var TValue $value */
$value = $this->option->unwrap();

return self::of($function($value));
} catch (Throwable $throwable) {
return Error::create($throwable);
}
Expand Down
11 changes: 0 additions & 11 deletions src/Contract/Exception/ResultExceptionInterface.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Ghostwriter\Result;

use Ghostwriter\Result\Contract\ErrorInterface;
use Throwable;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/ErrorInterface.php → src/ErrorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ghostwriter\Result\Contract;
namespace Ghostwriter\Result;

use Throwable;

Expand Down
8 changes: 2 additions & 6 deletions src/Exception/ResultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

namespace Ghostwriter\Result\Exception;

use Ghostwriter\Result\Contract\Exception\ResultExceptionInterface;
use Ghostwriter\Result\ExceptionInterface;
use RuntimeException;

final class ResultException extends RuntimeException implements ResultExceptionInterface
final class ResultException extends RuntimeException implements ExceptionInterface
{
public static function invalidMethodCall(string $method, string $type): self
{
return new self(sprintf('Invalid method call "%s()" on a Result of type %s', $method, $type));
}
}
11 changes: 11 additions & 0 deletions src/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Ghostwriter\Result;

use Throwable;

interface ExceptionInterface extends Throwable
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ghostwriter\Result\Contract;
namespace Ghostwriter\Result;

use Ghostwriter\Option\OptionInterface;
use Throwable;
Expand Down
2 changes: 0 additions & 2 deletions src/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Ghostwriter\Result;

use Ghostwriter\Result\Contract\SuccessInterface;

/**
* Represents the result of successful operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ghostwriter\Result\Contract;
namespace Ghostwriter\Result;

/**
* @template TValue
Expand Down
Loading

0 comments on commit e922fe4

Please sign in to comment.