Tests / PHP 8.4 (push) Failing after 5m34s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Fix the type errors PHPStan 2 reports: class-string<static> variance, optional array offsets in fromEnvironment, list vs array for PdoConnection::$lru, ReflectionClass<Model> docblocks, Repository made covariant in T, and a redundant null comparison in validateRelationPath. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
287 lines
9.2 KiB
PHP
287 lines
9.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Camelia\Orm\Metadata;
|
|
|
|
use Camelia\Orm\Attribute\AutoIncrement;
|
|
use Camelia\Orm\Attribute\BelongsTo;
|
|
use Camelia\Orm\Attribute\BelongsToMany;
|
|
use Camelia\Orm\Attribute\Column;
|
|
use Camelia\Orm\Attribute\Enum;
|
|
use Camelia\Orm\Attribute\Guarded;
|
|
use Camelia\Orm\Attribute\HasMany;
|
|
use Camelia\Orm\Attribute\HasOne;
|
|
use Camelia\Orm\Attribute\Ignore;
|
|
use Camelia\Orm\Attribute\Index;
|
|
use Camelia\Orm\Attribute\MaxLength;
|
|
use Camelia\Orm\Attribute\Nullable;
|
|
use Camelia\Orm\Attribute\PrimaryKey;
|
|
use Camelia\Orm\Attribute\Required;
|
|
use Camelia\Orm\Attribute\TableName;
|
|
use Camelia\Orm\Exception\MetadataException;
|
|
use Camelia\Orm\Model;
|
|
use Camelia\Orm\Support\NameConverter;
|
|
use ReflectionClass;
|
|
use ReflectionNamedType;
|
|
use ReflectionProperty;
|
|
|
|
/**
|
|
* Builds ClassMetadata from a model class through reflection.
|
|
*
|
|
* @internal
|
|
*/
|
|
final class MetadataFactory
|
|
{
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
*/
|
|
public function forClass(string $class): ClassMetadata
|
|
{
|
|
if (!class_exists($class) || !is_subclass_of($class, Model::class)) {
|
|
throw MetadataException::notAModel($class);
|
|
}
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
$tableName = $reflection->getAttributes(TableName::class)[0] ?? null;
|
|
|
|
if ($tableName === null) {
|
|
throw MetadataException::missingTableName($class);
|
|
}
|
|
|
|
/** @var TableName $table */
|
|
$table = $tableName->newInstance();
|
|
|
|
if (trim($table->name) === '') {
|
|
throw MetadataException::missingTableName($class);
|
|
}
|
|
|
|
$properties = [];
|
|
$primaryKeys = [];
|
|
$columnOwners = [];
|
|
|
|
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
|
|
if ($property->isStatic()) {
|
|
continue;
|
|
}
|
|
|
|
$declaring = $property->getDeclaringClass()->getName();
|
|
|
|
if ($declaring !== $class && $declaring === Model::class) {
|
|
continue;
|
|
}
|
|
|
|
$metadata = $this->buildProperty($class, $reflection, $property);
|
|
|
|
if (isset($columnOwners[$metadata->column])) {
|
|
throw MetadataException::duplicateColumn(
|
|
$class,
|
|
$metadata->column,
|
|
$columnOwners[$metadata->column],
|
|
$metadata->name,
|
|
);
|
|
}
|
|
|
|
$columnOwners[$metadata->column] = $metadata->name;
|
|
$properties[$metadata->name] = $metadata;
|
|
|
|
if ($metadata->primaryKey) {
|
|
$primaryKeys[] = $metadata->name;
|
|
}
|
|
}
|
|
|
|
if (count($primaryKeys) !== 1) {
|
|
throw MetadataException::primaryKeyCount($class, count($primaryKeys));
|
|
}
|
|
|
|
$primaryKey = $primaryKeys[0];
|
|
$primaryKeyProperty = $properties[$primaryKey] ?? null;
|
|
|
|
if ($primaryKeyProperty === null) {
|
|
throw MetadataException::unknownProperty($class, $primaryKey);
|
|
}
|
|
|
|
return new ClassMetadata(
|
|
$class,
|
|
$table->name,
|
|
$primaryKey,
|
|
$primaryKeyProperty->column,
|
|
$properties,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
* @param ReflectionClass<\Camelia\Orm\Model> $reflection
|
|
*/
|
|
private function buildProperty(
|
|
string $class,
|
|
ReflectionClass $reflection,
|
|
ReflectionProperty $property,
|
|
): PropertyMetadata {
|
|
$name = $property->getName();
|
|
$type = $property->getType();
|
|
|
|
if (!$type instanceof ReflectionNamedType) {
|
|
throw MetadataException::missingType($class, $name);
|
|
}
|
|
|
|
$relation = $this->buildRelation($class, $reflection, $property);
|
|
|
|
$columnAttribute = $property->getAttributes(Column::class)[0] ?? null;
|
|
$column = $columnAttribute !== null
|
|
? $columnAttribute->newInstance()->name
|
|
: NameConverter::camelToSnake($name);
|
|
|
|
if (preg_match('/[^\x20-\x7E]/', $column) === 1) {
|
|
throw MetadataException::nonAsciiColumn($class, $name, $column);
|
|
}
|
|
|
|
$enumAttribute = $property->getAttributes(Enum::class)[0] ?? null;
|
|
$enumClass = $enumAttribute !== null ? $enumAttribute->newInstance()->class : null;
|
|
|
|
if ($relation !== null) {
|
|
$this->assertRelationShape($class, $property, $relation);
|
|
}
|
|
|
|
$maxLength = $property->getAttributes(MaxLength::class)[0] ?? null;
|
|
|
|
return new PropertyMetadata(
|
|
name: $name,
|
|
column: $column,
|
|
type: $enumClass ?? $type->getName(),
|
|
enumClass: $enumClass,
|
|
relation: $relation,
|
|
declaringClass: $class,
|
|
constraint: new Constraint(
|
|
nullable: $type->allowsNull() || $property->getAttributes(Nullable::class) !== [],
|
|
ignored: $property->getAttributes(Ignore::class) !== [] || $relation !== null,
|
|
guarded: $property->getAttributes(Guarded::class) !== [],
|
|
primaryKey: $property->getAttributes(PrimaryKey::class) !== [],
|
|
autoIncrement: $property->getAttributes(AutoIncrement::class) !== [],
|
|
required: $property->getAttributes(Required::class) !== [],
|
|
indexed: $property->getAttributes(Index::class) !== [],
|
|
maxLength: $maxLength !== null ? $maxLength->newInstance()->value : null,
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
* @param ReflectionClass<\Camelia\Orm\Model> $reflection
|
|
*/
|
|
private function buildRelation(
|
|
string $class,
|
|
ReflectionClass $reflection,
|
|
ReflectionProperty $property,
|
|
): ?RelationMetadata {
|
|
$name = $property->getName();
|
|
|
|
$belongsTo = $property->getAttributes(BelongsTo::class)[0] ?? null;
|
|
|
|
if ($belongsTo !== null) {
|
|
/** @var BelongsTo $attribute */
|
|
$attribute = $belongsTo->newInstance();
|
|
|
|
return new RelationMetadata(
|
|
RelationMetadata::KIND_BELONGS_TO,
|
|
$this->resolveRelatedClass($class, $reflection, $attribute->related, $name),
|
|
$attribute->foreignKey,
|
|
);
|
|
}
|
|
|
|
$hasOne = $property->getAttributes(HasOne::class)[0] ?? null;
|
|
|
|
if ($hasOne !== null) {
|
|
/** @var HasOne $attribute */
|
|
$attribute = $hasOne->newInstance();
|
|
|
|
return new RelationMetadata(
|
|
RelationMetadata::KIND_HAS_ONE,
|
|
$this->resolveRelatedClass($class, $reflection, $attribute->related, $name),
|
|
$attribute->foreignKey,
|
|
);
|
|
}
|
|
|
|
$hasMany = $property->getAttributes(HasMany::class)[0] ?? null;
|
|
|
|
if ($hasMany !== null) {
|
|
/** @var HasMany $attribute */
|
|
$attribute = $hasMany->newInstance();
|
|
|
|
return new RelationMetadata(
|
|
RelationMetadata::KIND_HAS_MANY,
|
|
$this->resolveRelatedClass($class, $reflection, $attribute->related, $name),
|
|
$attribute->foreignKey,
|
|
);
|
|
}
|
|
|
|
$belongsToMany = $property->getAttributes(BelongsToMany::class)[0] ?? null;
|
|
|
|
if ($belongsToMany !== null) {
|
|
/** @var BelongsToMany $attribute */
|
|
$attribute = $belongsToMany->newInstance();
|
|
|
|
return new RelationMetadata(
|
|
RelationMetadata::KIND_BELONGS_TO_MANY,
|
|
$this->resolveRelatedClass($class, $reflection, $attribute->related, $name),
|
|
'',
|
|
$attribute->through,
|
|
$attribute->foreignPivotKey,
|
|
$attribute->relatedPivotKey,
|
|
);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param class-string<\Camelia\Orm\Model> $class
|
|
* @param ReflectionClass<\Camelia\Orm\Model> $reflection
|
|
* @return class-string<\Camelia\Orm\Model>
|
|
*/
|
|
private function resolveRelatedClass(
|
|
string $class,
|
|
ReflectionClass $reflection,
|
|
string $related,
|
|
string $property,
|
|
): string {
|
|
if (class_exists($related) && is_subclass_of($related, Model::class)) {
|
|
return $related;
|
|
}
|
|
|
|
$namespace = $reflection->getNamespaceName();
|
|
$candidate = $namespace === '' ? $related : $namespace . '\\' . $related;
|
|
|
|
if (class_exists($candidate) && is_subclass_of($candidate, Model::class)) {
|
|
return $candidate;
|
|
}
|
|
|
|
throw MetadataException::unknownRelatedClass($class, $property, $related);
|
|
}
|
|
|
|
private function assertRelationShape(
|
|
string $class,
|
|
ReflectionProperty $property,
|
|
RelationMetadata $relation,
|
|
): void {
|
|
$name = $property->getName();
|
|
$type = $property->getType();
|
|
|
|
if (!$type instanceof ReflectionNamedType) {
|
|
throw MetadataException::invalidRelationProperty($class, $name);
|
|
}
|
|
|
|
$isArray = $type->getName() === 'array';
|
|
|
|
if ($relation->isToMany() && !$isArray) {
|
|
throw MetadataException::invalidRelationProperty($class, $name);
|
|
}
|
|
|
|
if (!$relation->isToMany() && (!$type->allowsNull() || $isArray)) {
|
|
throw MetadataException::invalidRelationProperty($class, $name);
|
|
}
|
|
}
|
|
}
|