Skip to content
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
27 changes: 27 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Rector

on:
- pull_request

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pint:
name: Rector
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.4"
coverage: none
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Composer install
run: composer install --no-interaction --no-ansi --no-progress
- name: Run Rector
run: ./vendor/bin/rector process --dry-run
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"description": "",
"scripts": {
"phpstan": "php ./vendor/bin/phpstan analyse --memory-limit=4G",
"pint": "./vendor/bin/pint --test -v",
"pint:fix": "./vendor/bin/pint",
"test": "./vendor/bin/pest",
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --min=63.2",
"test:type-coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --type-coverage --min=99.8"
"pint": "./vendor/bin/pint --test -v --parallel",
"pint:fix": "./vendor/bin/pint -v --parallel",
"test": "./vendor/bin/pest --parallel",
"test:coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --parallel --min=84.6",
"test:type-coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --type-coverage --min=100"
},
"authors": [
{
Expand Down
1 change: 1 addition & 0 deletions pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"concat_space": {
"spacing": "one"
},
"declare_strict_types": true,
"fully_qualified_strict_types": true,
"function_to_constant": true,
"general_phpdoc_annotation_remove": {
Expand Down
28 changes: 9 additions & 19 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
earlyReturn: true,
strictBooleans: true,
)
->withPhpSets()
->withAttributesSets(phpunit: true)
->withRules([
Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector::class,
Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector::class,
Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector::class,
Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector::class,
Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector::class,
Rector\Php80\Rector\Class_\StringableForToStringRector::class,
Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector::class,
Rector\CodingStyle\Rector\Closure\StaticClosureRector::class,
Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector::class,
Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromBooleanStrictReturnsRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector::class,
Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class,
Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector::class,
Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector::class,
Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector::class,
Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector::class,
])
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withTypeCoverageLevel(0);
->withImportNames(removeUnusedImports: true);
2 changes: 2 additions & 0 deletions src/Attribute/OneToMany.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm\Attribute;

use Attribute;
Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/TableIndex.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm\Attribute;

use Attribute;
Expand Down
2 changes: 2 additions & 0 deletions src/Attribute/TablePrimary.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm\Attribute;

use Artemeon\Database\Schema\DataType;
Expand Down
4 changes: 3 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm;

use Artemeon\Database\ConnectionInterface;
Expand All @@ -17,7 +19,7 @@ class Collection extends AbstractLazyCollection
public function __construct(
private readonly string $relationTable,
private readonly string $sourceColumn,
private array $type,
private readonly array $type,
private readonly string $primaryValue,
private readonly ConnectionInterface $connection,
private readonly FieldMapper $mapper,
Expand Down
2 changes: 2 additions & 0 deletions src/Comparator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm;

use RuntimeException;
Expand Down
30 changes: 21 additions & 9 deletions src/Condition.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm;

use Artemeon\Orm\Condition\CompositeCondition;
Expand Down Expand Up @@ -47,7 +49,7 @@ public function getWhere(): string

public function isEmpty(): bool
{
return empty($this->where);
return $this->where === '' || $this->where === '0';
}

/**
Expand All @@ -56,29 +58,39 @@ public function isEmpty(): bool
final public static function forValue(mixed $value, string $tableColumn, ?Comparator $comparator = null): ?ConditionInterface
{
if (is_string($value)) {
if ($comparator === null || $comparator === Comparator::LIKE) {
if (!$comparator instanceof Comparator || $comparator === Comparator::LIKE) {
return new LikeCondition($tableColumn, '%' . $value . '%');
}

return new Condition($tableColumn . ' ' . $comparator->toSql() . ' ?', [$value]);
} elseif (is_int($value) || is_float($value)) {
if ($comparator === null || $comparator === Comparator::EQUAL) {
}

if (is_int($value) || is_float($value)) {
if (!$comparator instanceof Comparator || $comparator === Comparator::EQUAL) {
return new EqualsCondition($tableColumn, $value);
}

return new Condition($tableColumn . ' ' . $comparator->toSql() . ' ?', [$value]);
} elseif (is_bool($value)) {
if ($comparator === null || $comparator === Comparator::EQUAL) {
}

if (is_bool($value)) {
if (!$comparator instanceof Comparator || $comparator === Comparator::EQUAL) {
return new EqualsCondition($tableColumn, $value ? 1 : 0);
}

return new Condition($tableColumn . ' ' . $comparator->toSql() . ' ?', [$value]);
} elseif (null === $value) {
}

if (null === $value) {
return new IsNullCondition($tableColumn, $comparator === Comparator::IS_NOT_NULL);
} elseif (is_array($value)) {
}

if (is_array($value)) {
if ($comparator === Comparator::IN_OR_EMPTY) {
return new CompositeCondition([new InCondition($tableColumn, $value), new EmptyCondition($tableColumn)], Conjunction::OR);
} elseif ($comparator === Comparator::NOT_IN_OR_EMPTY) {
}

if ($comparator === Comparator::NOT_IN_OR_EMPTY) {
return new CompositeCondition([new InCondition($tableColumn, $value, true), new EmptyCondition($tableColumn)], Conjunction::OR);
}

Expand Down
19 changes: 7 additions & 12 deletions src/Condition/CompositeCondition.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm\Condition;

use Artemeon\Orm\ConditionInterface;
use Artemeon\Orm\Conjunction;

/**
* A orm condition to store several orm conditions.
* They will connect via given condition connect.
* e.g.
* ( (<restriction_1>) AND (<restriction_2>) AND (<restriction_3>) )
* ( (<restriction_1>) OR (<restriction_2>) OR (<restriction_3>) ).
* An orm condition to store several orm conditions.
* They will connect via the given conjunction.
*/
class CompositeCondition implements ConditionInterface
{
Expand Down Expand Up @@ -44,7 +43,7 @@ public function addCondition(ConditionInterface $condition): self

public function hasConditions(): bool
{
return count($this->conditions) > 0;
return $this->conditions !== [];
}

public function getWhere(): string
Expand All @@ -58,13 +57,9 @@ public function getWhere(): string
}

$result = '';
if (count($where) > 0) {
if ($where !== []) {
$result = implode(') ' . $this->conjunction->toSql() . ' (', $where);
if (count($where) == 1) {
$result = '(' . $result . ')';
} else {
$result = '( (' . $result . ') )';
}
$result = count($where) === 1 ? '(' . $result . ')' : '( (' . $result . ') )';
}

return $result;
Expand Down
30 changes: 24 additions & 6 deletions src/Condition/EmptyCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,45 @@

namespace Artemeon\Orm\Condition;

use Artemeon\Orm\Comparator;
use Artemeon\Orm\ConditionInterface;
use Artemeon\Orm\Conjunction;

use function sprintf;

class EmptyCondition implements ConditionInterface
readonly class EmptyCondition implements ConditionInterface
{
public function __construct(private string $columnName, private bool $negated = false)
{
public function __construct(
private string $columnName,
private bool $negated = false,
) {
}

public function getParams(): array
{
return [];
return [''];
}

public function getWhere(): string
{
if ($this->negated) {
return sprintf('%s IS NOT NULL AND %s != ?', $this->columnName, '');
return sprintf(
'%s %s %s %s %s ?',
$this->columnName,
Comparator::IS_NOT_NULL->toSql(),
Conjunction::AND->toSql(),
$this->columnName,
Comparator::NOT_EQUAL->toSql(),
);
}

return sprintf('%s IS NULL OR %s = ?', $this->columnName, '');
return sprintf(
'%s %s %s %s %s ?',
$this->columnName,
Comparator::IS_NULL->toSql(),
Conjunction::OR->toSql(),
$this->columnName,
Comparator::EQUAL->toSql(),
);
}
}
22 changes: 17 additions & 5 deletions src/Condition/EqualsCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

namespace Artemeon\Orm\Condition;

use Artemeon\Orm\Comparator;
use Artemeon\Orm\ConditionInterface;

use function sprintf;

class EqualsCondition implements ConditionInterface
readonly class EqualsCondition implements ConditionInterface
{
public function __construct(private readonly string $columnName, private readonly mixed $value, private readonly bool $negated = false)
{
public function __construct(
private string $columnName,
private mixed $value,
private bool $negated = false,
) {
}

public function getParams(): array
Expand All @@ -22,9 +26,17 @@ public function getParams(): array
public function getWhere(): string
{
if ($this->negated) {
return sprintf('%s != ?', $this->columnName);
return sprintf(
'%s %s ?',
$this->columnName,
Comparator::NOT_EQUAL->toSql(),
);
}

return sprintf('%s = ?', $this->columnName);
return sprintf(
'%s %s ?',
$this->columnName,
Comparator::EQUAL->toSql(),
);
}
}
13 changes: 13 additions & 0 deletions src/Condition/EqualsNotCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Artemeon\Orm\Condition;

readonly class EqualsNotCondition extends EqualsCondition
{
public function __construct(string $columnName, mixed $value)
{
parent::__construct($columnName, $value, true);
}
}
Loading