Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rafactor(fields): simplifies polymorphism and uses enumerations #55

Merged
merged 2 commits into from
Jul 30, 2023
Merged
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 @@ -22,7 +22,7 @@
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.1",
"rector/rector": "^0.16",
"rector/rector": "^0.17.7",
"soosyze/php-cs-fixer-config": "^1.0"
},
"suggest": {
Expand Down
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
]);

// is your PHP version different from the one your refactor to? [default: your PHP version], uses PHP_VERSION_ID format
$rectorConfig->phpVersion(PhpVersion::PHP_80);
$rectorConfig->phpVersion(PhpVersion::PHP_81);

// Define what rule sets will be applied
$rectorConfig->sets([
SetList::CODE_QUALITY,
SetList::PHP_80,
SetList::PHP_81,
SetList::DEAD_CODE,
SetList::TYPE_DECLARATION
]);
Expand Down
17 changes: 17 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);
/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/
namespace Soosyze\Queryflatfile;

/**
* @author Mathieu NOËL <[email protected]>
*/
class Command
{
public function __construct(readonly public string $name)
{
}
}
30 changes: 30 additions & 0 deletions src/Command/DropCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Command;

use Soosyze\Queryflatfile\Command;
use Soosyze\Queryflatfile\Enum\TableExecutionType;

/**
* @author Mathieu NOËL <[email protected]>
*/
final class DropCommand extends Command
{
public function __construct(readonly public string $name)
{
}

/**
* {@inheritdoc}
*/
public function getExecutionType(): TableExecutionType
{
return TableExecutionType::Drop;
}
}
30 changes: 30 additions & 0 deletions src/Command/RenameCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Command;

use Soosyze\Queryflatfile\Command;
use Soosyze\Queryflatfile\Enum\TableExecutionType;

/**
* @author Mathieu NOËL <[email protected]>
*/
final class RenameCommand extends Command
{
public function __construct(public readonly string $name, public readonly string $to)
{
}

/**
* {@inheritdoc}
*/
public function getExecutionType(): TableExecutionType
{
return TableExecutionType::Rename;
}
}
32 changes: 32 additions & 0 deletions src/Concern/Field/ThrowInvalidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Concern\Field;

/**
* @property string $name
*
* @author Mathieu NOËL <[email protected]>
*/
trait ThrowInvalidType
{
/**
* @throws \InvalidArgumentException
*/
public function throwInvalidType(mixed $value): never
{
throw new \InvalidArgumentException(
sprintf(
'The value of the %s field must be of type %s: %s given.',
$this->name,
$this->getType()->realType(),
gettype($value)
)
);
}
}
65 changes: 65 additions & 0 deletions src/Concern/Field/TryOrGetDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Concern\Field;

use Soosyze\Queryflatfile\Enum\CurentDefaultType;
use Soosyze\Queryflatfile\Exception\TableBuilder\ColumnsValueException;

/**
* @property string $name
*
* @author Mathieu NOËL <[email protected]>
*/
trait TryOrGetDate
{
use ThrowInvalidType;

private CurentDefaultType $currentDefault;

public function tryOrGetValue(mixed $value): string
{
if (!\is_string($value)) {
$this->throwInvalidType($value);
}

/** @var string $value */
if (strtolower($value) === $this->currentDefault->value) {
return $this->currentDefault->value;
}
if (($timestamp = strtotime($value))) {
return date($this->currentDefault->format(), $timestamp);
}

throw new ColumnsValueException(
sprintf('The value of the %s field must be a valid date: %s given', $this->name, $value)
);
}

/**
* {@inheritdoc}
*/
public function getValueDefault(): bool|float|int|string|null
{
if ($this->valueDefault !== null) {
if ($this->valueDefault === $this->currentDefault->value) {
return date($this->currentDefault->format(), time());
}

/* Si les variables magiques ne sont pas utilisé alors la vrais valeur par defaut est retourné. */
return $this->valueDefault;
}
if ($this->isNullable) {
return null;
}

throw new ColumnsValueException(
sprintf('%s not nullable or not default.', $this->name)
);
}
}
44 changes: 44 additions & 0 deletions src/Concern/Field/TryOrGetString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Concern\Field;

/**
* @property string $name
* @property int $length
*
* @author Mathieu NOËL <[email protected]>
*/
trait TryOrGetString
{
use ThrowInvalidType;

/**
* {@inheritdoc}
*/
public function tryOrGetValue(mixed $value): string
{
if (!\is_string($value)) {
$this->throwInvalidType($value);
}

/** @var string $value */
if (strlen($value) > $this->length) {
throw new \LengthException(
sprintf(
'The value of the %s field must be less than or equal to %s characters: %s given',
$this->name,
$this->length,
strlen($value)
)
);
}

return $value;
}
}
2 changes: 1 addition & 1 deletion src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
abstract class Driver implements DriverInterface
{
const DS = DIRECTORY_SEPARATOR;
public final const DS = DIRECTORY_SEPARATOR;

/**
* Déclenche une exception si l'extension du fichier n'est pas chargée.
Expand Down
26 changes: 26 additions & 0 deletions src/Enum/CurentDefaultType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Enum;

/**
* @author Mathieu NOËL <[email protected]>
*/
enum CurentDefaultType: string
{
case Date = 'current_date';
case DateTime = 'current_datetime';

public function format(): string
{
return match ($this) {
self::Date => 'Y-m-d',
self::DateTime => 'Y-m-d H:i:s',
};
}
}
61 changes: 61 additions & 0 deletions src/Enum/FieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/**
* @license https://github.com/soosyze/queryflatfile/blob/master/LICENSE (MIT License)
*/

namespace Soosyze\Queryflatfile\Enum;

/**
* @author Mathieu NOËL <[email protected]>
*/
enum FieldType: string
{
case Boolean = 'boolean';
case Char = 'char';
case DateTime = 'datetime';
case Date = 'date';
case Float = 'float';
case Increment = 'increments';
case Int = 'integer';
case String = 'string';
case Text = 'text';

private const TEXT_TYPES = [self::Text, self::String, self::Char];

private const DATE_TYPES = [self::Date, self::DateTime, self::Int];

private const NUMBER_TYPES = [self::Float, self::Increment, self::Int];

public function realType(): string
{
return match ($this) {
self::Boolean => 'boolean',
self::Char => 'string',
self::DateTime => 'string',
self::Date => 'string',
self::Float => 'float',
self::Int => 'integer',
self::Increment => 'integer',
self::String => 'string',
self::Text => 'string',
};
}

public function isModify(FieldType $newType): bool
{
return match ($this) {
self::Boolean => in_array($newType, [self::Boolean, ...self::TEXT_TYPES]),
self::Char => in_array($newType, self::TEXT_TYPES),
self::DateTime => in_array($newType, self::DATE_TYPES),
self::Date => in_array($newType, self::DATE_TYPES),
self::Float => in_array($newType, self::NUMBER_TYPES),
self::Int => in_array($newType, self::NUMBER_TYPES),
self::Increment => in_array($newType, self::NUMBER_TYPES),
self::String => in_array($newType, self::TEXT_TYPES),
self::Text => in_array($newType, self::TEXT_TYPES),
};
}
}
Loading
Loading