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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
},
"require": {
"artemeon/database": "^4.0",
"artemeon/database": "^4.0.1",
"doctrine/collections": "^2.0",
"php": ">=8.4",
"psr/simple-cache": "^3.0"
Expand All @@ -44,6 +44,7 @@
"symfony/cache": "^6.0"
},
"config": {
"process-timeout": 0,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
Expand Down
14 changes: 0 additions & 14 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Artemeon\\\\Database\\\\ConnectionInterface\\:\\:flushPreparedStatementsCache\\(\\)\\.$#"
count: 1
path: tests/EntityManagerTestCase.php

-
message: "#^Call to an undefined method Artemeon\\\\Database\\\\ConnectionInterface\\:\\:flushQueryCache\\(\\)\\.$#"
count: 1
path: tests/EntityManagerTestCase.php

-
message: "#^Call to an undefined method Artemeon\\\\Database\\\\ConnectionInterface\\:\\:flushTablesCache\\(\\)\\.$#"
count: 1
path: tests/EntityManagerTestCase.php
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- phpstan-baseline.neon

parameters:
level: 5
level: 6
paths:
- src
- tests
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
codingStyle: true,
typeDeclarations: true,
earlyReturn: true,
strictBooleans: true,
)
->withPhpSets()
->withAttributesSets(phpunit: true)
Expand Down
3 changes: 3 additions & 0 deletions src/Attribute/OneToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
class OneToMany
{
/**
* @param list<class-string> $type
*/
public function __construct(
public string $relationTable,
public string $sourceColumn,
Expand Down
3 changes: 3 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class Collection extends AbstractLazyCollection
{
/**
* @param list<class-string> $type
*/
public function __construct(
private readonly string $relationTable,
private readonly string $sourceColumn,
Expand Down
9 changes: 9 additions & 0 deletions src/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ class Condition implements ConditionInterface
{
protected string $where = '';

/**
* @var list<mixed>
*/
protected array $params = [];

/**
* @param list<mixed> $params
*/
public function __construct(string $where, array $params = [])
{
$this->setWhere($where);
$this->setParams($params);
}

/**
* @param list<mixed> $params
*/
public function setParams(array $params): void
{
$this->params = $params;
Expand Down
3 changes: 3 additions & 0 deletions src/Condition/InCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
public const int MAX_IN_VALUES = 950;

/**
* @param list<mixed> $params
*/
public function __construct(
private string $columnName,
private array $params,
Expand Down
2 changes: 2 additions & 0 deletions src/ConditionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function getWhere(): string;

/**
* Returns an array of the params for the given condition.
*
* @return list<mixed>
*/
public function getParams(): array;
}
22 changes: 19 additions & 3 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function __construct(private readonly ConnectionInterface $connection, pr
}

/**
* @param array<ConditionInterface> $conditions
* @param array<OrderByInterface> $sorting
* @param list<ConditionInterface> $conditions
* @param list<OrderByInterface> $sorting
*
* @throws OrmException
*
Expand Down Expand Up @@ -83,6 +83,12 @@ public function getCount(string $targetClass, array $conditions): int
return (int) $row['cnt'];
}

/**
* @param list<ConditionInterface> $conditions
* @param list<OrderByInterface> $sorting
*
* @return array{string, list<mixed>}
*/
private function getQuery(string $targetClass, array $conditions = [], array $sorting = []): array
{
$from = $this->queryBuilder->buildFrom($targetClass);
Expand Down Expand Up @@ -243,6 +249,13 @@ public function delete(EntityInterface $entity): void
$this->connection->transactionCommit();
}

/**
* @param list<mixed> $config
*
* @throws OrmException
*
* @return array{DoctrineCollection<int,object>,string,string,string,list<class-string>}
*/
private function getRelation(EntityInterface $entity, array $config): array
{
[$type, $class, $setter, $getter, $relationTable, $sourceColumn, $targetColumn, $types] = $config;
Expand All @@ -260,6 +273,9 @@ private function getRelation(EntityInterface $entity, array $config): array
return [$value, $relationTable, $sourceColumn, $targetColumn, $types];
}

/**
* @param list<array{DoctrineCollection<int,object>,string,string,string,list<class-string>}> $relations
*/
private function handleRelations(EntityInterface $entity, array $relations): void
{
$sourcePrimaryId = $this->entityMeta->getPrimaryId($entity);
Expand All @@ -270,7 +286,7 @@ private function handleRelations(EntityInterface $entity, array $relations): voi
$this->connection->delete($relationTable, [$sourceColumn => $sourcePrimaryId]);
foreach ($collection as $relationEntity) {
$relationEntityId = $this->entityMeta->getPrimaryId($relationEntity);
if ($relationEntityId === null || $relationEntityId === '' || $relationEntityId === '0') {
if (in_array($relationEntityId, [null, '', '0'], true)) {
$relationEntityId = $this->insert($relationEntity);
}

Expand Down
34 changes: 34 additions & 0 deletions src/EntityMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct(private readonly CacheInterface $cache)
{
}

/**
* @param class-string $entityClass
*
* @return array<string, mixed>
*/
public function getProperties(string $entityClass): array
{
$cacheKey = 'entity-meta-properties-' . str_replace('\\', '-', $entityClass);
Expand Down Expand Up @@ -72,6 +77,11 @@ public function getPrimaryId(EntityInterface $entity): ?string
return null;
}

/**
* @param class-string $entityClass
*
* @return array<class-string, string>
*/
public function getTableNames(string $entityClass): array
{
$cacheKey = 'entity-meta-table-names-' . str_replace('\\', '-', $entityClass);
Expand All @@ -85,6 +95,11 @@ public function getTableNames(string $entityClass): array
return $types;
}

/**
* @param class-string $entityClass
*
* @return array<class-string, string>
*/
private function getTableNamesFromEntity(string $entityClass): array
{
$class = new ReflectionClass($entityClass);
Expand All @@ -107,6 +122,16 @@ private function getTableNamesFromEntity(string $entityClass): array
return $result;
}

/**
* @param class-string $entityClass
*
* @return array<string, array{
* self::TYPE_*,
* class-string,
* non-empty-string,
* non-empty-string,
* }>
*/
private function getTypesFromEntity(string $entityClass): array
{
$class = new ReflectionClass($entityClass);
Expand Down Expand Up @@ -166,6 +191,9 @@ private function findTableColumnAttribute(ReflectionProperty $property): ?TableC
return null;
}

/**
* @param ReflectionClass<object> $class
*/
private function findTableNameAttribute(ReflectionClass $class): ?TableName
{
foreach ($class->getAttributes() as $attribute) {
Expand Down Expand Up @@ -210,6 +238,9 @@ private function getTypeHintForProperty(ReflectionProperty $property): ?string
return $type->getName();
}

/**
* @param ReflectionClass<object> $class
*/
private function getSetter(ReflectionClass $class, string $propertyName): ?string
{
$setter = null;
Expand Down Expand Up @@ -238,6 +269,9 @@ private function getSetter(ReflectionClass $class, string $propertyName): ?strin
return $setter;
}

/**
* @param ReflectionClass<object> $class
*/
private function getGetter(ReflectionClass $class, string $propertyName): ?string
{
$getter = null;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function __construct(
$this->queryBuilder = new QueryBuilder($this->connection, $this->entityMeta);
}

/**
* @param array<string, mixed> $row
*/
public function map(EntityInterface $entity, array $row): void
{
$sourcePrimaryColumn = $this->entityMeta->getPrimaryColumn($entity::class);
Expand Down
3 changes: 3 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
class QueryBuilder
{
/**
* @var list<non-empty-string>
*/
private array $blockedTableAlias = ['user'];

public function __construct(
Expand Down
7 changes: 7 additions & 0 deletions src/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function createTable(string $entityClass): void
}
}

/**
* @param class-string $entityClass
* @param list<mixed> $keys
* @param array<array-key, mixed> $relationTables
*
* @return array<string, list<mixed>>
*/
private function getFieldsForEntity(string $entityClass, array &$keys, array &$relationTables): array
{
$properties = $this->entityMeta->getProperties($entityClass);
Expand Down
3 changes: 3 additions & 0 deletions tests/Condition/CompositeConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function testSingleCondition(): void
self::assertSame(['bar'], $condition->getParams());
}

/**
* @return iterable<array{Conjunction}>
*/
public static function multipleConditionsProvider(): iterable
{
foreach (Conjunction::cases() as $conjunction) {
Expand Down
9 changes: 9 additions & 0 deletions tests/FieldMapper/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class TestModel extends TestParent
#[TableColumn('purchasing_relevance', DataType::INT)]
private ?int $purchasingRelevance = 0;

/**
* @var Collection<int, TestParent>|null
*/
#[OneToMany('agp_contracts_con2foo', 'contract_id', 'system_id', [TestParent::class])]
private ?Collection $relations = null;

Expand Down Expand Up @@ -82,11 +85,17 @@ public function setPurchasingRelevance(?int $purchasingRelevance): void
$this->purchasingRelevance = $purchasingRelevance;
}

/**
* @return Collection<int, TestParent>|null
*/
public function getRelations(): ?Collection
{
return $this->relations;
}

/**
* @param Collection<int, TestParent>|null $relations
*/
public function setRelations(?Collection $relations): void
{
$this->relations = $relations;
Expand Down
9 changes: 9 additions & 0 deletions tests/FieldMapper/TestModelWithTooLongRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class TestModelWithTooLongRelation implements EntityInterface
#[TablePrimary('my_id')]
private string $myId;

/**
* @var Collection<int, TestParent>|null
*/
#[OneToMany('some_really_long__relation_name', 'contract_id', 'system_id', [TestParent::class])]
private ?Collection $relations = null;

Expand All @@ -29,11 +32,17 @@ public function setMyId(string $myId): void
$this->myId = $myId;
}

/**
* @return Collection<int, TestParent>|null
*/
public function getRelations(): ?Collection
{
return $this->relations;
}

/**
* @param Collection<int, TestParent>|null $relations
*/
public function setRelations(?Collection $relations): void
{
$this->relations = $relations;
Expand Down
14 changes: 14 additions & 0 deletions tests/FieldMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Artemeon\Orm\Converter;
use Artemeon\Orm\EntityInterface;
use Artemeon\Orm\EntityMeta;
use Artemeon\Orm\Exception\OrmException;
use Artemeon\Orm\FieldMapper;
use Artemeon\Orm\Tests\FieldMapper\TestModel;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -20,6 +21,12 @@
*/
class FieldMapperTest extends TestCase
{
/**
* @param array<non-empty-string, mixed> $row
* @param array<non-empty-string, mixed> $expects
*
* @throws OrmException
*/
#[DataProvider('mapDataProvider')]
public static function testMap(EntityInterface $entity, array $row, array $expects): void
{
Expand All @@ -32,6 +39,13 @@ public static function testMap(EntityInterface $entity, array $row, array $expec
}
}

/**
* @return array{
* object,
* array<non-empty-string, mixed>,
* array<non-empty-string, mixed>,
* }[]
*/
public static function mapDataProvider(): array
{
return [
Expand Down