Skip to content

Commit d9c7904

Browse files
committed
Add return types for methods of classes implementing native PHP ones (e.g Iterator)
Includes some smaller modernizations of code
1 parent 360d56f commit d9c7904

7 files changed

Lines changed: 77 additions & 35 deletions

File tree

src/DataStreams/BaseDataStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ abstract public function save($filename);
3030
*
3131
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
3232
*/
33-
abstract public function current();
33+
abstract public function current():mixed;
3434
}

src/DataStreams/DefaultDataStream.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function __destruct()
3333
fclose($this->fopenResource);
3434
}
3535

36-
public function rewind()
36+
/**
37+
* @throws \Exception
38+
*/
39+
public function rewind():void
3740
{
3841
if ($this->currentLineNumber == 0) {
3942
// starting iterations
@@ -50,22 +53,22 @@ public function save($filename)
5053
fclose($target);
5154
}
5255

53-
public function current()
56+
public function current():mixed
5457
{
5558
return fgets($this->fopenResource);
5659
}
5760

58-
public function key()
61+
public function key():mixed
5962
{
6063
return $this->currentLineNumber;
6164
}
6265

63-
public function next()
66+
public function next():void
6467
{
6568
++$this->currentLineNumber;
6669
}
6770

68-
public function valid()
71+
public function valid():bool
6972
{
7073
return !feof($this->fopenResource);
7174
}

src/DataStreams/TabularDataStream.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class TabularDataStream extends BaseDataStream
1515
public $table;
1616
public $schema;
1717

18+
/**
19+
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
20+
* @throws \Exception
21+
*/
1822
public function __construct($dataSource, $dataSourceOptions = null)
1923
{
2024
parent::__construct($dataSource, $dataSourceOptions);
@@ -38,7 +42,7 @@ protected function getDataSourceObject()
3842
return new CsvDataSource($this->dataSource);
3943
}
4044

41-
public function rewind()
45+
public function rewind():void
4246
{
4347
$this->table->rewind();
4448
}
@@ -53,28 +57,26 @@ public function save($filename)
5357
*
5458
* @throws DataStreamValidationException
5559
*/
56-
public function current()
60+
public function current():mixed
5761
{
5862
try {
5963
return $this->table->current();
60-
} catch (DataSourceException $e) {
61-
throw new DataStreamValidationException($e->getMessage());
62-
} catch (FieldValidationException $e) {
64+
} catch (DataSourceException|FieldValidationException $e) {
6365
throw new DataStreamValidationException($e->getMessage());
6466
}
6567
}
6668

67-
public function key()
69+
public function key():mixed
6870
{
6971
return $this->table->key();
7072
}
7173

72-
public function next()
74+
public function next():void
7375
{
7476
$this->table->next();
7577
}
7678

77-
public function valid()
79+
public function valid(): bool
7880
{
7981
return $this->table->valid();
8082
}

src/Datapackages/BaseDatapackage.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace frictionlessdata\datapackage\Datapackages;
44

5+
use Exception;
56
use frictionlessdata\datapackage\Factory;
67
use frictionlessdata\datapackage\Package;
78
use frictionlessdata\datapackage\Registry;
89
use frictionlessdata\datapackage\Utils;
910
use frictionlessdata\datapackage\Validators\DatapackageValidator;
1011
use frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException;
1112
use frictionlessdata\datapackage\Exceptions\DatapackageInvalidSourceException;
13+
use Iterator;
1214
use ZipArchive;
1315

14-
abstract class BaseDatapackage implements \Iterator
16+
abstract class BaseDatapackage implements Iterator
1517
{
1618

1719
/**
@@ -34,6 +36,9 @@ public function __construct($descriptor, $basePath = null, $skipValidations = fa
3436
}
3537
}
3638

39+
/**
40+
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
41+
*/
3742
public static function create($name, $resources, $basePath = null)
3843
{
3944
$datapackage = new static((object) [
@@ -47,6 +52,9 @@ public static function create($name, $resources, $basePath = null)
4752
return $datapackage;
4853
}
4954

55+
/**
56+
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
57+
*/
5058
public function revalidate()
5159
{
5260
$this->rewind();
@@ -81,16 +89,23 @@ public function resources()
8189
return $resources;
8290
}
8391

92+
/**
93+
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
94+
* @throws \Exception
95+
*/
8496
public function getResource($name)
8597
{
8698
foreach ($this->descriptor->resources as $resourceDescriptor) {
8799
if ($resourceDescriptor->name == $name) {
88100
return $this->initResource($resourceDescriptor);
89101
}
90102
}
91-
throw new \Exception("couldn't find matching resource with name = '{$name}'");
103+
throw new Exception("couldn't find matching resource with name = '{$name}'");
92104
}
93105

106+
/**
107+
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
108+
*/
94109
public function addResource($name, $resource)
95110
{
96111
if (is_a($resource, 'frictionlessdata\\datapackage\\Resources\\BaseResource')) {
@@ -119,6 +134,10 @@ public function addResource($name, $resource)
119134
}
120135

121136
// TODO: remove this function and use the getResource / addResource directly (will need to modify a lot of tests code)
137+
138+
/**
139+
* @throws \Exception
140+
*/
122141
public function resource($name, $resource = null)
123142
{
124143
if ($resource) {
@@ -128,6 +147,9 @@ public function resource($name, $resource = null)
128147
}
129148
}
130149

150+
/**
151+
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
152+
*/
131153
public function removeResource($name)
132154
{
133155
$resourceDescriptors = [];
@@ -148,27 +170,30 @@ public function saveDescriptor($filename)
148170
}
149171

150172
// standard iterator functions - to iterate over the resources
151-
public function rewind()
173+
public function rewind():void
152174
{
153175
$this->currentResourcePosition = 0;
154176
}
155177

156-
public function current()
178+
/**
179+
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
180+
*/
181+
public function current():mixed
157182
{
158183
return $this->initResource($this->descriptor()->resources[$this->currentResourcePosition]);
159184
}
160185

161-
public function key()
186+
public function key():mixed
162187
{
163188
return $this->currentResourcePosition;
164189
}
165190

166-
public function next()
191+
public function next():void
167192
{
168193
++$this->currentResourcePosition;
169194
}
170195

171-
public function valid()
196+
public function valid():bool
172197
{
173198
return isset($this->descriptor()->resources[$this->currentResourcePosition]);
174199
}
@@ -242,7 +267,6 @@ protected function copy()
242267
* @param object $descriptor
243268
*
244269
* @return \frictionlessdata\datapackage\Resources\BaseResource
245-
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
246270
*/
247271
protected function initResource($descriptor)
248272
{

src/Resources/BaseResource.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use frictionlessdata\datapackage\Validators\ResourceValidator;
99
use frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException;
1010
use frictionlessdata\datapackage\Utils;
11+
use frictionlessdata\tableschema\SchemaValidationError;
1112

1213
abstract class BaseResource implements \Iterator
1314
{
@@ -146,11 +147,11 @@ public function isRemote()
146147

147148
public function data()
148149
{
149-
return isset($this->descriptor()->data) ? $this->descriptor()->data : null;
150+
return $this->descriptor()->data ?? null;
150151
}
151152

152153
// standard iterator functions - to iterate over the data sources
153-
public function rewind()
154+
public function rewind():void
154155
{
155156
$this->dataStreams = null;
156157
$this->currentDataStream = 0;
@@ -159,22 +160,25 @@ public function rewind()
159160
}
160161
}
161162

162-
public function current()
163+
/**
164+
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
165+
*/
166+
public function current():mixed
163167
{
164168
return $this->dataStreams()[$this->currentDataStream]->current();
165169
}
166170

167-
public function key()
171+
public function key():mixed
168172
{
169173
return $this->dataStreams()[$this->currentDataStream]->key();
170174
}
171175

172-
public function next()
176+
public function next():void
173177
{
174-
return $this->dataStreams()[$this->currentDataStream]->next();
178+
$this->dataStreams()[$this->currentDataStream]->next();
175179
}
176180

177-
public function valid()
181+
public function valid():bool
178182
{
179183
$dataStreams = $this->dataStreams();
180184
if ($dataStreams[$this->currentDataStream]->valid()) {
@@ -223,7 +227,7 @@ public static function validateDataSource($dataSource, $basePath = null)
223227
$dataSource = static::normalizeDataSource($dataSource, $basePath);
224228
if (!Utils::isHttpSource($dataSource) && !file_exists($dataSource)) {
225229
$errors[] = new ResourceValidationError(
226-
ResourceValidationError::SCHEMA_VIOLATION,
230+
SchemaValidationError::SCHEMA_VIOLATION,
227231
"data source file does not exist or is not readable: {$dataSource}"
228232
);
229233
}

src/Resources/TabularResource.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ protected function getDataStream($dataSource, $dataSourceOptions = null)
3131
{
3232
$dataSourceOptions = array_merge([
3333
'schema' => $this->schema(),
34-
'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null,
34+
'dialect' => $this->descriptor()->dialect ?? null,
3535
], (array) $dataSourceOptions);
3636

3737
return new TabularDataStream($this->normalizeDataSource($dataSource, $this->basePath), $dataSourceOptions);
3838
}
3939

40+
/**
41+
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
42+
*/
4043
protected function getInlineDataStream($data)
4144
{
4245
return new TabularInlineDataStream($data, [
4346
'schema' => $this->schema(),
44-
'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null,
47+
'dialect' => $this->descriptor()->dialect ?? null,
4548
]);
4649
}
4750

src/Validators/BaseValidator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ protected function getValidationProfile()
4444
return $this->descriptor->profile;
4545
}
4646

47+
/**
48+
* @throws \Exception
49+
*/
4750
protected function convertValidationSchemaFilenameToUrl($filename)
4851
{
4952
$filename = realpath($filename);
@@ -116,9 +119,12 @@ protected function getValidationErrorMessage(array $error)
116119
return sprintf('[%s] %s', $error['property'], $error['message']);
117120
}
118121

119-
/**
120-
* Does the validation, adds errors to the validator object using _addError method.
121-
*/
122+
/**
123+
* Does the validation, adds errors to the validator object using _addError
124+
* method.
125+
*
126+
* @throws \Exception
127+
*/
122128
protected function validateSchema()
123129
{
124130
$this->validateSchemaUrl($this->getValidationSchemaUrl());

0 commit comments

Comments
 (0)