Skip to content

Commit

Permalink
refactor: abstracts are now deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed May 13, 2024
1 parent f0a20c9 commit ece139d
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 65 deletions.
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
"allow-plugins": false,
"sort-packages": true
},
"conflict": {
"petrknap/shorts": "<2.1.1"
},
"description": "Optional (like in Java Platform SE 8 but in PHP)",
"funding": [
{
Expand All @@ -31,11 +28,11 @@
"license": "LGPL-3.0-or-later",
"name": "petrknap/optional",
"require": {
"php": ">=8.1",
"petrknap/shorts": "^2.1"
"php": ">=8.1"
},
"require-dev": {
"nunomaduro/phpinsights": "^2.11",
"petrknap/shorts": "^2.1",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5",
"squizlabs/php_codesniffer": "^3.7"
Expand Down
3 changes: 3 additions & 0 deletions src/AbstractOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Throwable;

/**
* @deprecated use {@see Optional}
*
* @todo merge it with {@see Optional}
* @todo make constructor protected
*
* @template T of mixed type of non-null value
Expand Down
11 changes: 8 additions & 3 deletions src/AbstractOptionalObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
namespace PetrKnap\Optional;

/**
* @deprecated use {@see OptionalObject}
*
* @todo merge it with {@see OptionalObject}
*
* @template T of object
*
* @template-extends AbstractOptional<T>
* @template-extends Optional<T>
*/
abstract class AbstractOptionalObject extends AbstractOptional
abstract class AbstractOptionalObject extends Optional
{
protected static function isSupported(mixed $value): bool
{
/** @var string $expectedObjectClassName */
$expectedObjectClassName = static::getObjectClassName();
return $value instanceof $expectedObjectClassName;
return is_object($value) && ($expectedObjectClassName === '' || $value instanceof $expectedObjectClassName);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/AbstractOptionalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
namespace PetrKnap\Optional;

/**
* @template-extends AbstractOptional<resource>
* @deprecated use {@see OptionalResource}
*
* @todo merge it with {@see OptionalResource}
*
* @template-extends Optional<resource>
*/
abstract class AbstractOptionalResource extends AbstractOptional
abstract class AbstractOptionalResource extends Optional
{
protected static function isSupported(mixed $value): bool
{
/** @var string $expectedResourceType */
$expectedResourceType = static::getResourceType();
return is_resource($value) && get_resource_type($value) === $expectedResourceType;
return is_resource($value) && ($expectedResourceType === '' || get_resource_type($value) === $expectedResourceType);
}

/**
Expand Down
19 changes: 7 additions & 12 deletions src/Optional.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
<?php

/**
* Please use another implementation of {@see Optional} if possible.
*/

declare(strict_types=1);

namespace PetrKnap\Optional;

use InvalidArgumentException;
use Throwable;

/**
* Please use another implementation of {@see AbstractOptional} if possible.
*
* @todo make it final
*
* @deprecated will be converted to final
*
* @template T of mixed
*
* @template-extends AbstractOptional<T>
*/
class Optional extends AbstractOptional
/* abstract */ class Optional extends AbstractOptional
{
protected static function isSupported(mixed $value): bool
/* abstract */ protected static function isSupported(mixed $value): bool
{
trigger_error(
static::class . ' does not check the type of value.',
error_level: E_USER_NOTICE,
);
return $value !== null;
return true;
}
}
4 changes: 2 additions & 2 deletions src/OptionalArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* @template K of array-key
* @template V of mixed
*
* @template-extends AbstractOptional<array<K, V>>
* @template-extends Optional<array<K, V>>
*/
final class OptionalArray extends AbstractOptional
final class OptionalArray extends Optional
{
protected static function isSupported(mixed $value): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/OptionalBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PetrKnap\Optional;

/**
* @template-extends AbstractOptional<bool>
* @template-extends Optional<bool>
*/
final class OptionalBool extends AbstractOptional
final class OptionalBool extends Optional
{
protected static function isSupported(mixed $value): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/OptionalFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PetrKnap\Optional;

/**
* @template-extends AbstractOptional<float>
* @template-extends Optional<float>
*/
final class OptionalFloat extends AbstractOptional
final class OptionalFloat extends Optional
{
protected static function isSupported(mixed $value): bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/OptionalInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PetrKnap\Optional;

/**
* @template-extends AbstractOptional<int>
* @template-extends Optional<int>
*/
final class OptionalInt extends AbstractOptional
final class OptionalInt extends Optional
{
protected static function isSupported(mixed $value): bool
{
Expand Down
22 changes: 9 additions & 13 deletions src/OptionalObject.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
<?php

/**
* Please use another implementation of {@see OptionalObject} if possible.
*/

declare(strict_types=1);

namespace PetrKnap\Optional;

use PetrKnap\Shorts\Exception\NotImplemented;

/**
* Please use another implementation of {@see AbstractOptionalObject} if possible.
*
* @template T of object
*
* @template-extends AbstractOptionalObject<object>
*/
final class OptionalObject extends AbstractOptionalObject
/* abstract */ class OptionalObject extends AbstractOptionalObject
{
protected static function isSupported(mixed $value): bool
/* abstract */ protected static function getObjectClassName(): string
{
trigger_error(
self::class . ' does not check the instance of object.',
static::class . ' does not check the instance of object.',
error_level: E_USER_NOTICE,
);
return is_object($value);
}

protected static function getObjectClassName(): string
{
NotImplemented::throw(__METHOD__);
/** @var class-string */
return '';
}
}
6 changes: 3 additions & 3 deletions src/OptionalObject/OptionalStdClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace PetrKnap\Optional\OptionalObject;

use PetrKnap\Optional\AbstractOptionalObject;
use PetrKnap\Optional\OptionalObject;
use stdClass;

/**
* @template-extends AbstractOptionalObject<stdClass>
* @template-extends OptionalObject<stdClass>
*/
final class OptionalStdClass extends AbstractOptionalObject
final class OptionalStdClass extends OptionalObject
{
protected static function getObjectClassName(): string
{
Expand Down
23 changes: 9 additions & 14 deletions src/OptionalResource.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<?php

/**
* Please use another implementation of {@see OptionalResource} if possible.
*/

declare(strict_types=1);

namespace PetrKnap\Optional;

use PetrKnap\Shorts\Exception\NotImplemented;

/**
* Please use another implementation of {@see AbstractOptionalResource} if possible.
*/
final class OptionalResource extends AbstractOptionalResource
/* abstract */ class OptionalResource extends AbstractOptionalResource
{
protected static function isSupported(mixed $value): bool
/* abstract */ protected static function getResourceType(): string
{
trigger_error(
self::class . ' does not check the type of resource.',
static::class . ' does not check the type of resource.',
error_level: E_USER_NOTICE,
);
return is_resource($value);
}

protected static function getResourceType(): string
{
NotImplemented::throw(__METHOD__);
/** @var non-empty-string */
return '';
}
}
4 changes: 2 additions & 2 deletions src/OptionalResource/OptionalStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace PetrKnap\Optional\OptionalResource;

use PetrKnap\Optional\AbstractOptionalResource;
use PetrKnap\Optional\OptionalResource;

final class OptionalStream extends AbstractOptionalResource
final class OptionalStream extends OptionalResource
{
protected static function getResourceType(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/OptionalString.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PetrKnap\Optional;

/**
* @template-extends AbstractOptional<string>
* @template-extends Optional<string>
*/
final class OptionalString extends AbstractOptional
final class OptionalString extends Optional
{
protected static function isSupported(mixed $value): bool
{
Expand Down

0 comments on commit ece139d

Please sign in to comment.