Skip to content

Commit dcd49d1

Browse files
committed
Refactor again for more code readability
1 parent e25b36c commit dcd49d1

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

src/lib/items/AttributeRelation.php

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10+
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;
1011
use yii\helpers\Inflector;
1112
use yii\helpers\VarDumper;
1213
use function reset;
13-
use cebe\yii2openapi\lib\traits\ForeignKeyConstraints;
1414

1515
class AttributeRelation
1616
{
@@ -19,41 +19,29 @@ class AttributeRelation
1919
public const HAS_ONE = 'hasOne';
2020
public const HAS_MANY = 'hasMany';
2121

22-
/**
23-
* @var string $name
24-
**/
25-
private $name;
22+
private string $name;
2623

27-
/**
28-
* @var string $tableName
29-
**/
30-
private $tableName;
24+
private ?string $tableName;
3125

32-
/**
33-
* @var string $className
34-
**/
35-
private $className;
26+
private ?string $className;
3627

3728
/**
38-
* @var string $method (hasOne/hasMany)
29+
* hasOne/hasMany
3930
**/
40-
private $method;
31+
private ?string $method;
4132

42-
/**
43-
* @var array
44-
**/
45-
private $link = [];
33+
private array $link;
4634

47-
/**@var bool */
48-
private $selfReference = false;
35+
private bool $selfReference = false;
4936

5037
public function __construct(
51-
string $name,
38+
string $name,
5239
?string $tableName = null,
5340
?string $className = null,
5441
?string $method = null,
55-
array $link = []
56-
) {
42+
array $link = []
43+
)
44+
{
5745
$this->name = $name;
5846
$this->tableName = $tableName;
5947
$this->className = $className;
@@ -65,7 +53,7 @@ public function __construct(
6553
* @param string $name
6654
* @return AttributeRelation
6755
*/
68-
public function setName(string $name):AttributeRelation
56+
public function setName(string $name): AttributeRelation
6957
{
7058
$this->name = $name;
7159
return $this;
@@ -75,7 +63,7 @@ public function setName(string $name):AttributeRelation
7563
* @param string $tableName
7664
* @return AttributeRelation
7765
*/
78-
public function setTableName(string $tableName):AttributeRelation
66+
public function setTableName(string $tableName): AttributeRelation
7967
{
8068
$this->tableName = $tableName;
8169
return $this;
@@ -85,108 +73,108 @@ public function setTableName(string $tableName):AttributeRelation
8573
* @param string $className
8674
* @return AttributeRelation
8775
*/
88-
public function setClassName(string $className):AttributeRelation
76+
public function setClassName(string $className): AttributeRelation
8977
{
9078
$this->className = $className;
9179
return $this;
9280
}
9381

94-
public function asSelfReference():AttributeRelation
82+
public function asSelfReference(): AttributeRelation
9583
{
9684
$this->selfReference = true;
9785
return $this;
9886
}
9987

100-
public function asHasOne(array $link):AttributeRelation
88+
public function asHasOne(array $link): AttributeRelation
10189
{
10290
$this->method = self::HAS_ONE;
10391
$this->link = $link;
10492
return $this;
10593
}
10694

107-
public function asHasMany(array $link):AttributeRelation
95+
public function asHasMany(array $link): AttributeRelation
10896
{
10997
$this->method = self::HAS_MANY;
11098
$this->link = $link;
11199
return $this;
112100
}
113101

114-
public function isHasOne():bool
102+
public function isHasOne(): bool
115103
{
116104
return $this->method === self::HAS_ONE;
117105
}
118106

119-
public function isSelfReferenced():bool
107+
public function isSelfReferenced(): bool
120108
{
121109
return $this->selfReference;
122110
}
123111

124112
/**
125113
* @return string
126114
*/
127-
public function getName():string
115+
public function getName(): string
128116
{
129117
return $this->name;
130118
}
131119

132120
/**
133121
* @return string
134122
*/
135-
public function getTableName():string
123+
public function getTableName(): string
136124
{
137125
return $this->tableName;
138126
}
139127

140-
public function getTableAlias():string
128+
public function getTableAlias(): string
141129
{
142130
return "{{%$this->tableName}}";
143131
}
144132

145133
/**
146134
* @return string
147135
*/
148-
public function getClassName():string
136+
public function getClassName(): string
149137
{
150138
return $this->className;
151139
}
152140

153-
public function getClassKey():string
141+
public function getClassKey(): string
154142
{
155143
return Inflector::camel2id($this->getClassName());
156144
}
157145

158146
/**
159147
* @return string
160148
*/
161-
public function getMethod():string
149+
public function getMethod(): string
162150
{
163151
return $this->method;
164152
}
165153

166154
/**
167155
* @return array
168156
*/
169-
public function getLink():array
157+
public function getLink(): array
170158
{
171159
return $this->link;
172160
}
173161

174-
public function getCamelName():string
162+
public function getCamelName(): string
175163
{
176164
return Inflector::camelize($this->name);
177165
}
178166

179-
public function getColumnName():string
167+
public function getColumnName(): string
180168
{
181169
return reset($this->link);
182170
}
183171

184-
public function getForeignName():string
172+
public function getForeignName(): string
185173
{
186174
return key($this->link);
187175
}
188176

189-
public function linkToString():string
177+
public function linkToString(): string
190178
{
191179
return str_replace(
192180
[',', '=>', ', ]'],

src/lib/items/DbModel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class DbModel extends BaseObject
5959
*/
6060
public array $many2many = [];
6161

62+
/**
63+
* @var array|AttributeRelation[] inverse relations # TODO
64+
*/
65+
public array $inverseRelations = [];
66+
6267
public array $junctionCols = [];
6368

6469
/**

src/lib/traits/ForeignKeyConstraints.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@
1010
trait ForeignKeyConstraints
1111
{
1212
/**
13-
* @var string
1413
* Contains foreign key constraint
1514
* @example 'SET NULL'
1615
* @example 'CASCADE'
1716
*/
18-
public $onDeleteFkConstraint;
17+
public string $onDeleteFkConstraint;
1918

2019
/**
21-
* @var string
2220
* Contains foreign key constraint
2321
* @example 'SET NULL'
2422
* @example 'CASCADE'
2523
*/
26-
public $onUpdateFkConstraint;
24+
public string $onUpdateFkConstraint;
2725
}

0 commit comments

Comments
 (0)