Skip to content

Commit cc3617c

Browse files
author
Simon Karlen
committed
added php 7 types
1 parent 3a69796 commit cc3617c

17 files changed

+94
-131
lines changed

src/ActiveFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function load()
7070
* @throws InvalidConfigException
7171
* @throws \ReflectionException
7272
*/
73-
protected function getData()
73+
protected function getData(): array
7474
{
7575
if ($this->dataFile === null) {
7676
if ($this->dataDirectory !== null) {

src/ActiveQuery.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
3838
* @throws \yii\db\Exception
3939
* @throws \yii\base\NotSupportedException
4040
*/
41-
public function createCommand($db = null)
41+
public function createCommand($db = null): Command
4242
{
4343
/**
4444
* @var ActiveRecord $modelClass
@@ -137,7 +137,7 @@ private function buildJoinWith()
137137
* @param ActiveRecord $model the primary model
138138
* @param array $with the relations to be joined
139139
*/
140-
protected function joinWithRelations($model, $with)
140+
protected function joinWithRelations(ActiveRecord $model, array $with)
141141
{
142142
foreach ($with as $name => $callback) {
143143
if (is_int($name)) {
@@ -167,7 +167,7 @@ protected function joinWithRelations($model, $with)
167167
* @param ActiveQuery $parent
168168
* @param ActiveQuery $child
169169
*/
170-
private function joinWithRelation($parent, $child)
170+
private function joinWithRelation(ActiveQuery $parent, ActiveQuery $child)
171171
{
172172
if (!empty($child->join)) {
173173
foreach ($child->join as $join) {
@@ -205,7 +205,7 @@ public function one($db = null)
205205
* {@inheritdoc}
206206
* @throws InvalidConfigException
207207
*/
208-
public function populate($rows)
208+
public function populate($rows): array
209209
{
210210
if (empty($rows)) {
211211
return [];
@@ -232,7 +232,7 @@ public function populate($rows)
232232
/**
233233
* {@inheritDoc}
234234
*/
235-
protected function createModels($rows)
235+
protected function createModels($rows): array
236236
{
237237
if ($this->asArray) {
238238
return $rows;
@@ -270,7 +270,7 @@ protected function createModels($rows)
270270
* @return array the distinctive models
271271
* @throws InvalidConfigException if model primary key is empty
272272
*/
273-
private function removeDuplicatedModels($models)
273+
private function removeDuplicatedModels(array $models): array
274274
{
275275
$hash = [];
276276
/* @var $class ActiveRecord */
@@ -352,7 +352,7 @@ private function removeDuplicatedModels($models)
352352
*
353353
* @return $this the query object itself
354354
*/
355-
public function joinWith($with)
355+
public function joinWith($with): ActiveQuery
356356
{
357357
$this->joinWith[] = (array)$with;
358358
return $this;

src/ActiveRecord.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ class ActiveRecord extends BaseActiveRecord
2525
*/
2626
private $_attributeFields = [];
2727

28-
/**
29-
* @var array
30-
*/
31-
private $_relations = [];
32-
3328
/**
3429
* @return array
3530
* @throws \ReflectionException
3631
*/
37-
public function attributes()
32+
public function attributes(): array
3833
{
3934
if (empty($this->_attributeFields)) {
4035
$regex = '#^@property(?:-(read|write))?(?:\s+([^\s]+))?\s+\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)#';
@@ -56,28 +51,6 @@ public function attributes()
5651
return $this->_attributeFields;
5752
}
5853

59-
/**
60-
* Relation getter
61-
* @return array
62-
*/
63-
public function getRelations()
64-
{
65-
if (empty($this->_relations)) {
66-
$regex = '#^@property-read(?:\s+([^\s \[\]]+)(?:\[\])?)?\s+\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)#';
67-
$reflection = new \ReflectionClass($this);
68-
$docLines = preg_split('~\R~u', $reflection->getDocComment());
69-
foreach ($docLines as $docLine) {
70-
$matches = [];
71-
$docLine = ltrim($docLine, "\t* ");
72-
if (preg_match($regex, $docLine, $matches) && isset($matches[2])) {
73-
$this->_relations[$matches[2]] = $matches[1];
74-
}
75-
}
76-
}
77-
78-
return $this->_relations;
79-
}
80-
8154
/**
8255
* {@inheritdoc}
8356
* @throws InvalidConfigException
@@ -93,7 +66,7 @@ public static function primaryKey()
9366
* @return ActiveQuery
9467
* @throws InvalidConfigException
9568
*/
96-
public static function find()
69+
public static function find(): ActiveQuery
9770
{
9871
/* @var $query ActiveQuery */
9972
$query = Yii::createObject(ActiveQuery::class, [get_called_class()]);
@@ -102,15 +75,15 @@ public static function find()
10275
}
10376

10477
/**
78+
* {@inheritDoc}
79+
*
10580
* @return null|Connection
10681
* @throws InvalidConfigException
10782
*/
10883
public static function getDb()
10984
{
110-
$connection = Yii::$app->get(Connection::getDriverName());
111-
11285
/* @var $connection Connection */
113-
return $connection;
86+
return Yii::$app->get(Connection::getDriverName());
11487
}
11588

11689
/**
@@ -124,7 +97,7 @@ public static function getDb()
12497
* @return string the url path
12598
* @throws InvalidConfigException
12699
*/
127-
public static function modelName()
100+
public static function modelName(): string
128101
{
129102
$path = Inflector::camel2id(StringHelper::basename(get_called_class()), '-');
130103
return static::getDb()->usePluralisation ? Inflector::pluralize($path) : $path;
@@ -135,7 +108,7 @@ public static function modelName()
135108
* @throws InvalidConfigException
136109
* @throws Exception
137110
*/
138-
public function insert($runValidation = true, $attributes = null)
111+
public function insert($runValidation = true, $attributes = null): bool
139112
{
140113
if ($runValidation && !$this->validate($attributes)) {
141114
Yii::info('Model not inserted due to validation error.', __METHOD__);
@@ -156,7 +129,7 @@ public function insert($runValidation = true, $attributes = null)
156129
* @throws InvalidConfigException
157130
* @throws Exception
158131
*/
159-
protected function insertInternal($attributes)
132+
protected function insertInternal(array $attributes): bool
160133
{
161134
if (!$this->beforeSave(true)) {
162135
return false;
@@ -179,7 +152,6 @@ protected function insertInternal($attributes)
179152
/**
180153
* {@inheritdoc}
181154
* @throws InvalidConfigException
182-
* @throws \yii\httpclient\Exception
183155
*/
184156
public function update($runValidation = true, $attributeNames = null)
185157
{

src/Command.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ class Command extends Component
5656

5757
/**
5858
* Enables query cache for this command.
59-
* @param int $duration the number of seconds that query result of this command can remain valid in the cache.
59+
* @param int|null $duration the number of seconds that query result of this command can remain valid in the cache.
6060
* If this is not set, the value of [[Connection::queryCacheDuration]] will be used instead.
6161
* Use 0 to indicate that the cached data will never expire.
62-
* @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query result.
62+
* @param \yii\caching\Dependency|null $dependency the cache dependency associated with the cached query result.
6363
* @return $this the command object itself
6464
*/
65-
public function cache($duration = null, $dependency = null)
65+
public function cache(int $duration = null, \yii\caching\Dependency $dependency = null): Command
6666
{
6767
$this->queryCacheDuration = $duration === null ? $this->db->queryCacheDuration : $duration;
6868
$this->queryCacheDependency = $dependency;
@@ -73,7 +73,7 @@ public function cache($duration = null, $dependency = null)
7373
* Disables query cache for this command.
7474
* @return $this the command object itself
7575
*/
76-
public function noCache()
76+
public function noCache(): Command
7777
{
7878
$this->queryCacheDuration = -1;
7979
return $this;
@@ -85,7 +85,7 @@ public function noCache()
8585
* It is likely that this method returns an invalid URL due to improper replacement of parameter placeholders.
8686
* @return string the raw URL with parameter values inserted into the corresponding placeholders.
8787
*/
88-
public function getRawUrl()
88+
public function getRawUrl(): string
8989
{
9090
$rawUrl = $this->db->handler->get($this->pathInfo, $this->queryParams)->fullUrl;
9191

@@ -94,29 +94,30 @@ public function getRawUrl()
9494

9595
/**
9696
* Executes the SQL statement and returns ALL rows at once.
97-
* @param int $fetchMode for compatibility with [[\yii\db\Command]]
97+
* @param int|null $fetchMode for compatibility with [[\yii\db\Command]]
9898
* @return array all rows of the query result. Each array element is an array representing a row of data.
9999
* An empty array is returned if the query results in nothing.
100100
* @throws \yii\base\InvalidConfigException
101101
*/
102-
public function queryAll($fetchMode = null)
102+
public function queryAll(int $fetchMode = null): array
103103
{
104104
return $this->queryInternal();
105105
}
106106

107107
/**
108108
* Executes the SQL statement and returns the first row of the result.
109109
* This method is best used when only the first row of result is needed for a query.
110-
* @param int $fetchMode for compatibility with [[\yii\db\Command]]
110+
* @param int|null $fetchMode for compatibility with [[\yii\db\Command]]
111111
* @return array|false the first row (in terms of an array) of the query result. False is returned if the query
112112
* results in nothing.
113+
* @throws \yii\base\InvalidConfigException
113114
*/
114-
public function queryOne($fetchMode = null)
115+
public function queryOne(int $fetchMode = null)
115116
{
116-
/* @var $class ActiveRecord */
117117
$class = $this->modelClass;
118118

119119
if (!empty($class) && class_exists($class)) {
120+
/* @var $class ActiveRecord */
120121
$pks = $class::primaryKey();
121122

122123
if (count($pks) === 1 && isset($this->queryParams['filter'])) {
@@ -138,7 +139,7 @@ public function queryOne($fetchMode = null)
138139
*
139140
* @return mixed
140141
*/
141-
public function execute($method = 'get')
142+
public function execute(string $method = 'get')
142143
{
143144
return $this->queryInternal($method);
144145
}
@@ -152,7 +153,7 @@ public function execute($method = 'get')
152153
* @return mixed
153154
* @throws Exception
154155
*/
155-
public function insert($model, $columns)
156+
public function insert(string $model, array $columns)
156157
{
157158
$this->pathInfo = $model;
158159

@@ -164,12 +165,11 @@ public function insert($model, $columns)
164165
*
165166
* @param string $model
166167
* @param array $data
167-
* @param string $id
168+
* @param string|null $id
168169
*
169170
* @return mixed
170-
* @throws Exception
171171
*/
172-
public function update($model, $data = [], $id = null)
172+
public function update(string $model, array $data = [], string $id = null)
173173
{
174174
$method = $this->db->updateMethod;
175175
$this->pathInfo = $model;
@@ -184,12 +184,12 @@ public function update($model, $data = [], $id = null)
184184
* Deletes a record
185185
*
186186
* @param string $model
187-
* @param string $id
187+
* @param string|null $id
188188
*
189189
* @return mixed
190190
* @throws Exception
191191
*/
192-
public function delete($model, $id = null)
192+
public function delete(string $model, string $id = null)
193193
{
194194
$this->pathInfo = $model;
195195
if ($id) {
@@ -206,7 +206,7 @@ public function delete($model, $id = null)
206206
*
207207
* @return mixed
208208
*/
209-
protected function queryInternal($method = 'get')
209+
protected function queryInternal(string $method = 'get')
210210
{
211211
if ($this->db->usePluralisation && strpos($this->pathInfo, '/') === false) {
212212
$this->pathInfo = Inflector::pluralize($this->pathInfo);
@@ -246,7 +246,7 @@ protected function queryInternal($method = 'get')
246246
* @return array the cache key
247247
* @since 2.0.16
248248
*/
249-
protected function getCacheKey($method)
249+
protected function getCacheKey(string $method): array
250250
{
251251
return [
252252
__CLASS__,

0 commit comments

Comments
 (0)