Skip to content

Commit d9bad2e

Browse files
committed
Polish
1 parent e8dd4af commit d9bad2e

File tree

4 files changed

+183
-188
lines changed

4 files changed

+183
-188
lines changed

src/lib/AttributeResolver.php

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace cebe\yii2openapi\lib;
99

10-
use cebe\yii2openapi\lib\Config;
11-
use cebe\yii2openapi\lib\CustomSpecAttr;
1210
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
1311
use cebe\yii2openapi\lib\items\Attribute;
1412
use cebe\yii2openapi\lib\items\AttributeRelation;
@@ -20,9 +18,9 @@
2018
use cebe\yii2openapi\lib\openapi\ComponentSchema;
2119
use cebe\yii2openapi\lib\openapi\PropertySchema;
2220
use Yii;
21+
use yii\base\InvalidConfigException;
2322
use yii\helpers\Inflector;
2423
use yii\helpers\StringHelper;
25-
use yii\helpers\VarDumper;
2624
use function explode;
2725
use function strpos;
2826
use function strtolower;
@@ -63,7 +61,7 @@ class AttributeResolver
6361
private $schema;
6462

6563
/**
66-
* @var \cebe\yii2openapi\lib\items\JunctionSchemas
64+
* @var JunctionSchemas
6765
*/
6866
private $junctions;
6967

@@ -88,14 +86,14 @@ public function __construct(string $schemaName, ComponentSchema $schema, Junctio
8886
}
8987

9088
/**
91-
* @return \cebe\yii2openapi\lib\items\DbModel
92-
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
93-
* @throws \yii\base\InvalidConfigException
89+
* @return DbModel
90+
* @throws InvalidDefinitionException
91+
* @throws InvalidConfigException
9492
*/
95-
public function resolve():DbModel
93+
public function resolve(): DbModel
9694
{
9795
foreach ($this->schema->getProperties() as $property) {
98-
/** @var $property \cebe\yii2openapi\lib\openapi\PropertySchema */
96+
/** @var $property PropertySchema */
9997

10098
$isRequired = $this->schema->isRequiredProperty($property->getName());
10199
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
@@ -130,25 +128,25 @@ public function resolve():DbModel
130128
}
131129

132130
/**
133-
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
134-
* @param bool $isRequired
135-
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
136-
* @throws \yii\base\InvalidConfigException
131+
* @param PropertySchema $property
132+
* @param bool $isRequired
133+
* @throws InvalidDefinitionException
134+
* @throws InvalidConfigException
137135
*/
138-
protected function resolveJunctionTableProperty(PropertySchema $property, bool $isRequired):void
136+
protected function resolveJunctionTableProperty(PropertySchema $property, bool $isRequired): void
139137
{
140138
if ($this->junctions->isJunctionProperty($this->schemaName, $property->getName())) {
141139
$junkAttribute = $this->junctions->byJunctionSchema($this->schemaName)[$property->getName()];
142140
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
143141
$attribute->setRequired($isRequired)
144-
->setDescription($property->getAttr('description', ''))
145-
->setReadOnly($property->isReadonly())
146-
->setIsPrimary($property->isPrimaryKey())
147-
->asReference($junkAttribute['relatedClassName'])
148-
->setPhpType($junkAttribute['phpType'])
149-
->setDbType($junkAttribute['dbType'])
150-
->setForeignKeyColumnName($property->fkColName)
151-
->setTableName($this->schema->resolveTableName($this->schemaName));
142+
->setDescription($property->getAttr('description', ''))
143+
->setReadOnly($property->isReadonly())
144+
->setIsPrimary($property->isPrimaryKey())
145+
->asReference($junkAttribute['relatedClassName'])
146+
->setPhpType($junkAttribute['phpType'])
147+
->setDbType($junkAttribute['dbType'])
148+
->setForeignKeyColumnName($property->fkColName)
149+
->setTableName($this->schema->resolveTableName($this->schemaName));
152150
$relation = Yii::createObject(AttributeRelation::class, [
153151
$property->getName(),
154152
$junkAttribute['relatedTableName'],
@@ -163,12 +161,12 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
163161
}
164162

165163
/**
166-
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
167-
* @param bool $isRequired
168-
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
169-
* @throws \yii\base\InvalidConfigException
164+
* @param PropertySchema $property
165+
* @param bool $isRequired
166+
* @throws InvalidDefinitionException
167+
* @throws InvalidConfigException
170168
*/
171-
protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bool $isRequired):void
169+
protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bool $isRequired): void
172170
{
173171
if ($this->junctions->isManyToManyProperty($this->schemaName, $property->getName())) {
174172
return;
@@ -198,44 +196,45 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
198196

199197
$this->relations[Inflector::pluralize($junkRef)] =
200198
Yii::createObject(AttributeRelation::class, [$junkRef, $junkAttribute['junctionTable'], $viaModel])
201-
->asHasMany([$junkAttribute['pairProperty'] . '_id' => $this->schema->getPkName()]);
199+
->asHasMany([$junkAttribute['pairProperty'] . '_id' => $this->schema->getPkName()]);
202200
return;
203201
}
204202

205203
$this->resolveProperty($property, $isRequired);
206204
}
207205

208206
/**
209-
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
210-
* @param bool $isRequired
211-
* @param bool|null|string $nullableValue if string then its value will be only constant `ARG_ABSENT`. Default `null` is avoided because it can be in passed value in method call
212-
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
213-
* @throws \yii\base\InvalidConfigException
207+
* @param PropertySchema $property
208+
* @param bool $isRequired
209+
* @param bool|null|string $nullableValue if string then its value will be only constant `ARG_ABSENT`. Default `null` is avoided because it can be in passed value in method call
210+
* @throws InvalidDefinitionException
211+
* @throws InvalidConfigException
214212
*/
215213
protected function resolveProperty(
216214
PropertySchema $property,
217-
bool $isRequired,
218-
$nullableValue = 'ARG_ABSENT'
219-
):void {
215+
bool $isRequired,
216+
$nullableValue = 'ARG_ABSENT'
217+
): void
218+
{
220219
if ($nullableValue === 'ARG_ABSENT') {
221220
$nullableValue = $property->getProperty()->getSerializableData()->nullable ?? null;
222221
}
223222
$attribute = Yii::createObject(Attribute::class, [$property->getName()]);
224223
$attribute->setRequired($isRequired)
225-
->setDescription($property->getAttr('description', ''))
226-
->setReadOnly($property->isReadonly())
227-
->setDefault($property->guessDefault())
228-
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
229-
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
230-
->setNullable($nullableValue)
231-
->setIsPrimary($property->isPrimaryKey())
232-
->setForeignKeyColumnName($property->fkColName)
233-
->setTableName($this->schema->resolveTableName($this->schemaName));
224+
->setDescription($property->getAttr('description', ''))
225+
->setReadOnly($property->isReadonly())
226+
->setDefault($property->guessDefault())
227+
->setXDbType($property->getAttr(CustomSpecAttr::DB_TYPE))
228+
->setXDbDefaultExpression($property->getAttr(CustomSpecAttr::DB_DEFAULT_EXPRESSION))
229+
->setNullable($nullableValue)
230+
->setIsPrimary($property->isPrimaryKey())
231+
->setForeignKeyColumnName($property->fkColName)
232+
->setTableName($this->schema->resolveTableName($this->schemaName));
234233
if ($property->isReference()) {
235234
if ($property->isVirtual()) {
236235
throw new InvalidDefinitionException('References not supported for virtual attributes');
237236
}
238-
237+
239238
if ($property->isNonDbReference()) {
240239
$attribute->asNonDbReference($property->getRefClassName());
241240
$relation = Yii::createObject(
@@ -260,17 +259,17 @@ protected function resolveProperty(
260259
[$min, $max] = $fkProperty->guessMinMax();
261260
$attribute->asReference($relatedClassName);
262261
$attribute->setPhpType($fkProperty->guessPhpType())
263-
->setDbType($fkProperty->guessDbType(true))
264-
->setSize($fkProperty->getMaxLength())
265-
->setDescription($property->getRefSchema()->getDescription())
266-
->setDefault($fkProperty->guessDefault())
267-
->setLimits($min, $max, $fkProperty->getMinLength());
262+
->setDbType($fkProperty->guessDbType(true))
263+
->setSize($fkProperty->getMaxLength())
264+
->setDescription($property->getRefSchema()->getDescription())
265+
->setDefault($fkProperty->guessDefault())
266+
->setLimits($min, $max, $fkProperty->getMinLength());
268267

269268
$relation = Yii::createObject(
270269
AttributeRelation::class,
271270
[$property->getName(), $relatedTableName, $relatedClassName]
272271
)
273-
->asHasOne([$fkProperty->getName() => $attribute->columnName]);
272+
->asHasOne([$fkProperty->getName() => $attribute->columnName]);
274273
$relation->onUpdateFkConstraint = $property->onUpdateFkConstraint;
275274
$relation->onDeleteFkConstraint = $property->onDeleteFkConstraint;
276275
if ($property->isRefPointerToSelf()) {
@@ -281,10 +280,10 @@ protected function resolveProperty(
281280
if (!$property->isReference() && !$property->hasRefItems()) {
282281
[$min, $max] = $property->guessMinMax();
283282
$attribute->setIsVirtual($property->isVirtual())
284-
->setPhpType($property->guessPhpType())
285-
->setDbType($property->guessDbType())
286-
->setSize($property->getMaxLength())
287-
->setLimits($min, $max, $property->getMinLength());
283+
->setPhpType($property->guessPhpType())
284+
->setDbType($property->guessDbType())
285+
->setSize($property->getMaxLength())
286+
->setLimits($min, $max, $property->getMinLength());
288287
if ($property->hasEnum()) {
289288
$attribute->setEnumValues($property->getAttr('enum'));
290289
}
@@ -321,7 +320,7 @@ protected function resolveProperty(
321320
AttributeRelation::class,
322321
[$property->getName(), $relatedTableName, $relatedClassName]
323322
)
324-
->asHasMany([$fkProperty->getName() => $fkProperty->getName()])->asSelfReference();
323+
->asHasMany([$fkProperty->getName() => $fkProperty->getName()])->asSelfReference();
325324
return;
326325
}
327326
$foreignPk = Inflector::camel2id($fkProperty->getName(), '_') . '_id';
@@ -330,7 +329,7 @@ protected function resolveProperty(
330329
AttributeRelation::class,
331330
[$property->getName(), $relatedTableName, $relatedClassName]
332331
)
333-
->asHasMany([$foreignPk => $this->schema->getPkName()]);
332+
->asHasMany([$foreignPk => $this->schema->getPkName()]);
334333
return;
335334
}
336335
$relatedClassName = $property->getRefClassName();
@@ -349,7 +348,7 @@ protected function resolveProperty(
349348
AttributeRelation::class,
350349
[$property->getName(), $relatedTableName, $relatedClassName]
351350
)
352-
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->schema->getPkName()]);
351+
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->schema->getPkName()]);
353352
return;
354353
}
355354
if ($this->schema->isNonDb() && $attribute->isReference()) {
@@ -369,14 +368,15 @@ protected function resolveProperty(
369368
* @param string $relatedTableName
370369
* @param ComponentSchema $refSchema
371370
* @return bool
372-
* @throws \yii\base\InvalidConfigException
371+
* @throws InvalidConfigException
373372
*/
374373
protected function catchManyToMany(
375-
string $propertyName,
376-
string $relatedSchemaName,
377-
string $relatedTableName,
374+
string $propertyName,
375+
string $relatedSchemaName,
376+
string $relatedTableName,
378377
ComponentSchema $refSchema
379-
):bool {
378+
): bool
379+
{
380380
if (strtolower(Inflector::id2camel($propertyName, '_'))
381381
!== strtolower(Inflector::pluralize($relatedSchemaName))) {
382382
return false;
@@ -408,9 +408,9 @@ protected function catchManyToMany(
408408
}
409409

410410
/**
411-
* @throws \yii\base\InvalidConfigException
411+
* @throws InvalidConfigException
412412
*/
413-
protected function guessFakerStub(Attribute $attribute, PropertySchema $property):?string
413+
protected function guessFakerStub(Attribute $attribute, PropertySchema $property): ?string
414414
{
415415
$resolver = Yii::createObject(['class' => FakerStubResolver::class], [$attribute, $property, $this->config]);
416416
return $resolver->resolve();
@@ -419,9 +419,9 @@ protected function guessFakerStub(Attribute $attribute, PropertySchema $property
419419
/**
420420
* @param array $indexes
421421
* @return array|DbIndex[]
422-
* @throws \cebe\yii2openapi\lib\exceptions\InvalidDefinitionException
422+
* @throws InvalidDefinitionException
423423
*/
424-
protected function prepareIndexes(array $indexes):array
424+
protected function prepareIndexes(array $indexes): array
425425
{
426426
$dbIndexes = [];
427427
foreach ($indexes as $index) {
@@ -472,12 +472,12 @@ protected function prepareIndexes(array $indexes):array
472472
}
473473

474474
/**
475-
* @param \cebe\yii2openapi\lib\openapi\PropertySchema $property
476-
* @param \cebe\yii2openapi\lib\items\Attribute $attribute
475+
* @param PropertySchema $property
476+
* @param Attribute $attribute
477477
* @return void
478-
* @throws \yii\base\InvalidConfigException
478+
* @throws InvalidConfigException
479479
*/
480-
protected function resolvePropertyRef(PropertySchema $property, Attribute $attribute):void
480+
protected function resolvePropertyRef(PropertySchema $property, Attribute $attribute): void
481481
{
482482
$fkProperty = new PropertySchema(
483483
$property->getRefSchema()->getSchema(),
@@ -486,11 +486,11 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
486486
);
487487
[$min, $max] = $fkProperty->guessMinMax();
488488
$attribute->setPhpType($fkProperty->guessPhpType())
489-
->setDbType($fkProperty->guessDbType(true))
490-
->setSize($fkProperty->getMaxLength())
491-
->setDescription($fkProperty->getAttr('description'))
492-
->setDefault($fkProperty->guessDefault())
493-
->setLimits($min, $max, $fkProperty->getMinLength());
489+
->setDbType($fkProperty->guessDbType(true))
490+
->setSize($fkProperty->getMaxLength())
491+
->setDescription($fkProperty->getAttr('description'))
492+
->setDefault($fkProperty->guessDefault())
493+
->setLimits($min, $max, $fkProperty->getMinLength());
494494
$this->attributes[$property->getName()] =
495495
$attribute->setFakerStub($this->guessFakerStub($attribute, $fkProperty));
496496
}

src/lib/items/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function setDbType(string $dbType):Attribute
150150
return $this;
151151
}
152152

153-
public function setTableName(string $tableName):Attribute
153+
public function setTableName(string $tableName): Attribute
154154
{
155155
$this->tableName = $tableName;
156156
return $this;

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,13 +598,10 @@ public function buildTablesDrop(): void
598598
return;
599599
}
600600

601-
// $this->migration->addDownCode($this->recordBuilder->addPrimaryKey($this->model->getTableAlias(), $this->tableSchema->primaryKey));
602-
603601
$this->migration->addUpCode($this->recordBuilder->dropTable($this->model->getTableAlias()))
604602
->addDownCode(
605603
$this->recordBuilder->createTable(
606604
$this->model->getTableAlias(),
607-
// $this->model->attributesToColumnSchema()
608605
SchemaToDatabase::enhanceColumnSchemas($this->tableSchema->columns)
609606
)
610607
);

0 commit comments

Comments
 (0)