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
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ jobs:
run: make php/tests # To generate the coverage report

- name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@v5.2.0
uses: SonarSource/sonarqube-scan-action@v6.0.0
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//~ Rules
->setRules(
[
'@PER-CS2.0' => true,
'@PER-CS3x0' => true,
]
)

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
},

"require-dev": {
"friendsofphp/php-cs-fixer": "^3.86.0",
"phpstan/phpstan": "^2.1.22",
"friendsofphp/php-cs-fixer": "^3.89.2",
"phpstan/phpstan": "^2.1.31",
"phpstan/phpstan-phpunit": "^2.0.7",
"phpstan/phpstan-strict-rules": "^2.0.6",
"phpunit/phpcov": "^11.0.1",
"phpunit/phpunit": "^12.3.6",
"symfony/cache": "^7.3.2",
"phpstan/phpstan-strict-rules": "^2.0.7",
"phpunit/phpcov": "^11.0.2",
"phpunit/phpunit": "^12.4.2",
"symfony/cache": "^7.3.6",
"shipmonk/composer-dependency-analyser": "^1.8.3"
}
}
2 changes: 1 addition & 1 deletion src/EntityAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function disableIgnoreNotMappedFields(): static;
* @param bool $exists
* @return TEntity
*/
public function newEntity(\stdClass|null $row = null, bool $exists = false): object;
public function newEntity(?\stdClass $row = null, bool $exists = false): object;

/**
* Create new entity from array.
Expand Down
2 changes: 1 addition & 1 deletion src/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function setExists(bool $exists): static;
* @param string|null $property
* @return bool
*/
public function isUpdated(string|null $property = null): bool;
public function isUpdated(?string $property = null): bool;

/**
* Reset updated list of properties
Expand Down
30 changes: 15 additions & 15 deletions src/Generator/Compiler/Field/FieldValidatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,21 @@ private function getValidatorOptionsFromType(Field $field, Type\TypeInterface $t
private function getIntegerOptions(string $typeClass, bool $isUnsigned): array
{
return match ($typeClass) {
Type\TypeBigint::class => $isUnsigned ?
IntegerValidator::BIGINT_UNSIGNED :
IntegerValidator::BIGINT_SIGNED,
Type\TypeInt::class => $isUnsigned ?
IntegerValidator::INT_UNSIGNED :
IntegerValidator::INT_SIGNED,
Type\TypeMediumint::class => $isUnsigned ?
IntegerValidator::MEDIUMINT_UNSIGNED :
IntegerValidator::MEDIUMINT_SIGNED,
Type\TypeSmallint::class => $isUnsigned ?
IntegerValidator::SMALLINT_UNSIGNED :
IntegerValidator::SMALLINT_SIGNED,
Type\TypeTinyint::class => $isUnsigned ?
IntegerValidator::TINYINT_UNSIGNED :
IntegerValidator::TINYINT_SIGNED,
Type\TypeBigint::class => $isUnsigned
? IntegerValidator::BIGINT_UNSIGNED
: IntegerValidator::BIGINT_SIGNED,
Type\TypeInt::class => $isUnsigned
? IntegerValidator::INT_UNSIGNED
: IntegerValidator::INT_SIGNED,
Type\TypeMediumint::class => $isUnsigned
? IntegerValidator::MEDIUMINT_UNSIGNED
: IntegerValidator::MEDIUMINT_SIGNED,
Type\TypeSmallint::class => $isUnsigned
? IntegerValidator::SMALLINT_UNSIGNED
: IntegerValidator::SMALLINT_SIGNED,
Type\TypeTinyint::class => $isUnsigned
? IntegerValidator::TINYINT_UNSIGNED
: IntegerValidator::TINYINT_SIGNED,
default => [],
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Templates/AbstractMapper.template
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ abstract class Abstract{{ class.mapper }}
public function __construct(
string $connectionName,
ConnectionFactory $connectionFactory,
ValidatorFactoryInterface|null $validatorFactory = null,
ValidatorEntityFactory|null $validatorEntityFactory = null,
?ValidatorFactoryInterface $validatorFactory = null,
?ValidatorEntityFactory $validatorEntityFactory = null,
array $mappers = [],
CacheItemPoolInterface|null $cache = null,
?CacheItemPoolInterface $cache = null,
bool $enableCacheOnRead = false,
) {
$this->setConnectionName($connectionName);
Expand Down
8 changes: 4 additions & 4 deletions src/Query/InsertBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function getQuery(bool $onDuplicateUpdate = false, bool $onDuplicateIgnor
$this->appendUpdateValues();
}

return 'INSERT ' . $onDuplicateIgnoreClause .
'INTO ' . $this->repository->getTable() .
$this->getQuerySet() .
$this->getQueryDuplicateUpdate()
return 'INSERT ' . $onDuplicateIgnoreClause
. 'INTO ' . $this->repository->getTable()
. $this->getQuerySet()
. $this->getQueryDuplicateUpdate()
;
}

Expand Down
22 changes: 11 additions & 11 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class QueryBuilder extends SelectBuilder
*/
public function getQuery(bool $usePrefix = false, string $prefix = '', bool $onlyPrimaryKey = false): string
{
return 'SELECT ' . $this->getQueryFieldsPersonalized() .
$this->getQueryFromPersonalized() .
$this->getQueryWhere() .
$this->getQueryGroupBy() .
$this->getQueryHaving() .
$this->getQueryOrderBy() .
$this->getQueryLimit()
return 'SELECT ' . $this->getQueryFieldsPersonalized()
. $this->getQueryFromPersonalized()
. $this->getQueryWhere()
. $this->getQueryGroupBy()
. $this->getQueryHaving()
. $this->getQueryOrderBy()
. $this->getQueryLimit()
;
}

Expand All @@ -43,10 +43,10 @@ public function getQuery(bool $usePrefix = false, string $prefix = '', bool $onl
*/
public function getQueryCount(string $field = '*'): string
{
return 'SELECT COUNT(' . $field . ') AS nb_results' .
$this->getQueryFrom($this->repository) .
$this->getQueryWhere() .
$this->getQueryGroupBy()
return 'SELECT COUNT(' . $field . ') AS nb_results'
. $this->getQueryFrom($this->repository)
. $this->getQueryWhere()
. $this->getQueryGroupBy()
;
}
}
16 changes: 8 additions & 8 deletions src/Query/SelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public function clear(bool $forNotCached = false): static
*/
public function getQuery(bool $usePrefix = false, string $prefix = '', bool $onlyPrimaryKey = false): string
{
return 'SELECT ' . $this->getQueryFields($this->repository, $usePrefix, $onlyPrimaryKey) .
$this->getQueryFrom($this->repository) .
$this->getQueryJoin() .
$this->getQueryWhere() .
$this->getQueryGroupBy() .
$this->getQueryHaving() .
$this->getQueryOrderBy() .
$this->getQueryLimit();
return 'SELECT ' . $this->getQueryFields($this->repository, $usePrefix, $onlyPrimaryKey)
. $this->getQueryFrom($this->repository)
. $this->getQueryJoin()
. $this->getQueryWhere()
. $this->getQueryGroupBy()
. $this->getQueryHaving()
. $this->getQueryOrderBy()
. $this->getQueryLimit();
}
}
5 changes: 5 additions & 0 deletions src/Query/UpdateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function getQuery(): string
continue;
}

//~ Skip fields that are not updated
if (!$this->repository->isEntityUpdated($this->entity, $field)) {
continue;
}

$this->addSet($field, $value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CacheAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function disableCacheOnRead(): static
* @param CacheItemPoolInterface|null $cache
* @return static
*/
protected function setCache(CacheItemPoolInterface|null $cache = null): static
protected function setCache(?CacheItemPoolInterface $cache = null): static
{
$this->cache = $cache;

Expand Down
4 changes: 1 addition & 3 deletions src/Traits/EntityAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

namespace Eureka\Component\Orm\Traits;

use Eureka\Component\Orm\EntityAwareInterface;
use Eureka\Component\Orm\EntityInterface;
use Eureka\Component\Orm\Enumerator\JoinType;
use Eureka\Component\Orm\RepositoryInterface;
use Eureka\Component\Validation\Entity\GenericEntity;

/**
Expand Down Expand Up @@ -61,7 +59,7 @@ public function setEntityClass(string $entityClass): static
*
* @phpstan-return TEntity
*/
public function newEntity(\stdClass|null $row = null, bool $exists = false): object
public function newEntity(?\stdClass $row = null, bool $exists = false): object
{
$entity = new $this->entityClass($this, $this->getValidatorFactory(), $this->getValidatorEntityFactory());

Expand Down
5 changes: 4 additions & 1 deletion src/Traits/MapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public function query(QueryBuilderInterface $queryBuilder): array
);
}

/** @var string|int $index */
$index = !empty($indexedBy) ? $row->{$indexedBy} : $id++;
$entity = $this->newEntity($row, true);
$collection[$index] = $entity;
Expand Down Expand Up @@ -501,7 +502,9 @@ private function setIndexFieldsOnCollection(string $listIndexedBy, array $rawCol
$nameMap = $this->getNamesMap($listIndexedBy);
$getter = $nameMap['get'];
foreach ($rawCollection as $item) {
$collection[$item->$getter()] = $item;
/** @var string|int $index */
$index = $item->$getter();
$collection[$index] = $item;
}

return $collection;
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/QueryBuilder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,33 @@ public function testIHaveAnExceptionWhenITryToGetQueryOnUpdateBuilderWithoutEnti
$queryBuilder->getQuery();
}

/**
* @return void
* @throws OrmException
*/
public function testIHavePartialQueryUpdateWhenOnlySomePropertyAreUpdated(): void
{
$repository = $this->getUserRepository($this->getMockEntityFindAll());
$user = $repository->newEntity(
(object) [
'user_id' => 1,
'user_is_enabled' => true,
'user_email' => 'user@example.com',
'user_password' => md5('password'),
'user_date_create' => '2020-01-01 10:00:00',
'user_date_update' => null,
],
true,
);

$user->setDateUpdate('2020-01-01 10:00:00');
$queryBuilder = new UpdateBuilder($repository, $user);

$suffix = '[a-z0-9]{13}';
$patternQuery = "#UPDATE user SET `user_date_update` = :user_date_update_$suffix WHERE user_id = :user_id_$suffix#";
self::assertMatchesRegularExpression($patternQuery, $queryBuilder->getQuery());
}

/**
* @param array<mixed> $entityMock
* @return ConnectionFactory
Expand Down