Skip to content

Commit 2e7b8d6

Browse files
committed
Add types to methods
1 parent 5fec5af commit 2e7b8d6

File tree

12 files changed

+108
-106
lines changed

12 files changed

+108
-106
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: byjg

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
"minimum-stability": "dev",
1616
"require": {
1717
"php": ">=8.1",
18+
"ext-dom": "*",
1819
"byjg/xmlutil": "^5.0",
1920
"byjg/serializer": "^5.0"
2021
},
2122
"suggest": {
22-
"ext-dom": "*"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^9.6",

src/AnyDataset.php

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use ByJG\AnyDataset\Core\Exception\DatabaseException;
66
use ByJG\AnyDataset\Core\Formatter\XmlFormatter;
7+
use ByJG\XmlUtil\Exception\FileException;
8+
use ByJG\XmlUtil\Exception\XmlUtilException;
79
use ByJG\XmlUtil\File;
810
use ByJG\XmlUtil\XmlDocument;
911
use ByJG\XmlUtil\XmlNode;
12+
use Closure;
1013
use DOMElement;
1114
use InvalidArgumentException;
1215

@@ -51,22 +54,22 @@ class AnyDataset
5154
*
5255
* @var Row[]
5356
*/
54-
private $collection;
57+
private array $collection;
5558

5659
/**
5760
* Current node anydataset works
5861
* @var int
5962
*/
60-
private $currentRow;
63+
private int $currentRow;
6164

6265
private ?File $file;
6366

6467
/**
65-
* @param null|string $filename
66-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
67-
* @throws \ByJG\XmlUtil\Exception\XmlUtilException
68+
* @param string|null $filename
69+
* @throws FileException
70+
* @throws XmlUtilException
6871
*/
69-
public function __construct($filename = null)
72+
public function __construct(?string $filename = null)
7073
{
7174
$this->collection = array();
7275
$this->currentRow = -1;
@@ -82,7 +85,7 @@ public function __construct($filename = null)
8285
/**
8386
* @return string|null
8487
*/
85-
public function getFilename()
88+
public function getFilename(): ?string
8689
{
8790
return $this->file->getFilename();
8891
}
@@ -92,12 +95,13 @@ public function getFilename()
9295
* @param string|null $filename
9396
* @param mixed $closure
9497
* @return void
98+
* @throws FileException
9599
*/
96-
private function defineSavePath($filename, $closure)
100+
private function defineSavePath(?string $filename, Closure $closure): void
97101
{
98102
if (!is_null($filename)) {
99103
$ext = pathinfo($filename, PATHINFO_EXTENSION);
100-
if (empty($ext) && substr($filename, 0, 6) !== "php://") {
104+
if (empty($ext) && !str_starts_with($filename, "php://")) {
101105
$filename .= '.anydata.xml';
102106
}
103107
$this->file = new File($filename, allowNotFound: true);
@@ -109,12 +113,10 @@ private function defineSavePath($filename, $closure)
109113
/**
110114
* Private method used to read and populate anydataset class from specified file
111115
*
112-
* @param string $filepath Path and Filename to be read
113116
* @return void
114-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
115-
* @throws \ByJG\XmlUtil\Exception\XmlUtilException
117+
* @throws XmlUtilException
116118
*/
117-
private function createFromFile()
119+
private function createFromFile(): void
118120
{
119121
if (file_exists($this->getFilename())) {
120122
$anyDataSet = new XmlDocument($this->file);
@@ -152,9 +154,9 @@ public function xml(): string
152154
* @param string|null $filename
153155
* @return void
154156
* @throws DatabaseException
155-
* @throws \ByJG\XmlUtil\Exception\XmlUtilException
157+
* @throws FileException
156158
*/
157-
public function save($filename = null)
159+
public function save(?string $filename = null): void
158160
{
159161
$this->defineSavePath($filename, function () use ($filename){
160162
if (is_null($this->file)) {
@@ -168,11 +170,10 @@ public function save($filename = null)
168170
/**
169171
* Append one row to AnyDataset.
170172
*
171-
* @param Row|array|\stdClass|object|null $singleRow
173+
* @param Row|array $singleRow
172174
* @return void
173-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
174175
*/
175-
public function appendRow($singleRow = [])
176+
public function appendRow(Row|array $singleRow = []): void
176177
{
177178
if (!empty($singleRow)) {
178179
if ($singleRow instanceof Row) {
@@ -196,9 +197,8 @@ public function appendRow($singleRow = [])
196197
*
197198
* @param GenericIterator $iterator
198199
* @return void
199-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
200200
*/
201-
public function import($iterator)
201+
public function import(GenericIterator $iterator): void
202202
{
203203
foreach ($iterator as $singleRow) {
204204
$this->appendRow($singleRow);
@@ -209,11 +209,9 @@ public function import($iterator)
209209
* Insert one row before specified position.
210210
*
211211
* @param int $rowNumber
212-
* @param Row|array|\stdClass|object $row
213-
* @return void
214-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
212+
* @param Row|array $row
215213
*/
216-
public function insertRowBefore($rowNumber, $row)
214+
public function insertRowBefore(int $rowNumber, Row|array $row): void
217215
{
218216
if ($rowNumber > count($this->collection)) {
219217
$this->appendRow($row);
@@ -236,11 +234,11 @@ public function insertRowBefore($rowNumber, $row)
236234

237235
/**
238236
*
239-
* @param mixed $row
237+
* @param int|Row|null $row
240238
* @return void
241239
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
242240
*/
243-
public function removeRow($row = null)
241+
public function removeRow(int|Row $row = null): void
244242
{
245243
if (is_null($row)) {
246244
$row = $this->currentRow;
@@ -270,9 +268,8 @@ public function removeRow($row = null)
270268
* @param string $name - Field name
271269
* @param string $value - Field value
272270
* @return void
273-
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
274271
*/
275-
public function addField($name, $value)
272+
public function addField(string $name, mixed $value): void
276273
{
277274
if ($this->currentRow < 0) {
278275
$this->appendRow();
@@ -282,10 +279,10 @@ public function addField($name, $value)
282279

283280
/**
284281
* Get an Iterator filtered by an IteratorFilter
285-
* @param IteratorFilter $itf
286-
* @return GenericIterator
282+
* @param IteratorFilter|null $itf
283+
* @return GenericIterator|AnyIterator
287284
*/
288-
public function getIterator(IteratorFilter $itf = null)
285+
public function getIterator(IteratorFilter $itf = null): GenericIterator|AnyIterator
289286
{
290287
if (is_null($itf)) {
291288
return new AnyIterator($this->collection);
@@ -298,10 +295,10 @@ public function getIterator(IteratorFilter $itf = null)
298295
* Undocumented function
299296
*
300297
* @param string $fieldName
301-
* @param IteratorFilter $itf
298+
* @param IteratorFilter|null $itf
302299
* @return array
303300
*/
304-
public function getArray($fieldName, $itf = null)
301+
public function getArray(string $fieldName, IteratorFilter $itf = null): array
305302
{
306303
$iterator = $this->getIterator($itf);
307304
$result = array();
@@ -316,7 +313,7 @@ public function getArray($fieldName, $itf = null)
316313
* @param string $field
317314
* @return void
318315
*/
319-
public function sort($field)
316+
public function sort(string $field): void
320317
{
321318
if (count($this->collection) == 0) {
322319
return;
@@ -330,7 +327,7 @@ public function sort($field)
330327
* @param string $field
331328
* @return array
332329
*/
333-
protected function quickSortExec($seq, $field)
330+
protected function quickSortExec(array $seq, string $field): array
334331
{
335332
if (!count($seq)) {
336333
return $seq;

src/AnyIterator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ public function __construct($list)
3434
/**
3535
* @inheritDoc
3636
*/
37-
public function count()
37+
public function count(): int
3838
{
3939
return count($this->list);
4040
}
4141

4242
/**
4343
* @inheritDoc
4444
*/
45-
public function hasNext()
45+
public function hasNext(): bool
4646
{
4747
return ($this->curRow < $this->count());
4848
}
4949

5050
/**
5151
* @inheritDoc
5252
*/
53-
public function moveNext()
53+
public function moveNext(): Row|null
5454
{
5555
if (!$this->hasNext()) {
5656
return null;

src/Formatter/BaseFormatter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
namespace ByJG\AnyDataset\Core\Formatter;
44

55
use ByJG\AnyDataset\Core\GenericIterator;
6-
use \ByJG\AnyDataset\Core\Row;
6+
use ByJG\AnyDataset\Core\Row;
77
use InvalidArgumentException;
88

99
abstract class BaseFormatter implements FormatterInterface
1010
{
1111
/**
1212
* @var GenericIterator|Row
1313
*/
14-
protected $object;
14+
protected Row|GenericIterator $object;
1515

1616
/**
1717
* @inheritDoc
1818
*/
19-
abstract public function raw();
19+
abstract public function raw(): mixed;
2020

2121
/**
2222
* @inheritDoc
2323
*/
24-
abstract public function toText();
24+
abstract public function toText(): string;
2525

2626
/**
2727
* @inheritDoc
2828
*/
29-
public function saveToFile($filename)
29+
public function saveToFile(string $filename): void
3030
{
3131
if (empty($filename)) {
3232
throw new InvalidArgumentException("Filename cannot be empty");
@@ -37,7 +37,7 @@ public function saveToFile($filename)
3737
/**
3838
* @param GenericIterator|Row $object
3939
*/
40-
public function __construct($object)
40+
public function __construct(GenericIterator|Row $object)
4141
{
4242
if (!($object instanceof GenericIterator) && !($object instanceof Row)) {
4343
throw new InvalidArgumentException("Constructor must have a GenericIterator or Row instance in the argument");

src/Formatter/FormatterInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ interface FormatterInterface
99
*
1010
* @return mixed
1111
*/
12-
public function raw();
12+
public function raw(): mixed;
1313

1414
/**
1515
* Return the object transformed to string.
1616
*
1717
* @return string
1818
*/
19-
public function toText();
19+
public function toText(): string;
2020

2121
/**
2222
* Save the contents to a file
2323
*
2424
* @param string $filename
2525
* @return void
2626
*/
27-
public function saveToFile($filename);
27+
public function saveToFile(string $filename): void;
2828
}

src/Formatter/JsonFormatter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
namespace ByJG\AnyDataset\Core\Formatter;
44

55
use ByJG\AnyDataset\Core\GenericIterator;
6+
use ByJG\Serializer\Exception\InvalidArgumentException;
67

78
class JsonFormatter extends BaseFormatter
89
{
910
/**
1011
* @inheritDoc
12+
* @throws InvalidArgumentException
1113
*/
12-
public function raw()
14+
public function raw(): mixed
1315
{
1416
return json_decode($this->toText());
1517
}
1618

1719
/**
1820
* @inheritDoc
21+
* @throws InvalidArgumentException
1922
*/
20-
public function toText()
23+
public function toText(): string
2124
{
2225
if ($this->object instanceof GenericIterator) {
2326
return json_encode($this->object->toArray());

src/Formatter/XmlFormatter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
use ByJG\AnyDataset\Core\GenericIterator;
66
use ByJG\XmlUtil\XmlDocument;
7+
use ByJG\XmlUtil\XmlNode;
78

89
class XmlFormatter extends BaseFormatter
910
{
1011
/**
1112
* Return a DOMNode representing AnyDataset
1213
*
1314
* @param array $collection
14-
* @return XmlDocument
15+
* @return XmlNode
1516
*/
16-
protected function anydatasetXml($collection)
17+
protected function anydatasetXml(array $collection): XmlNode
1718
{
1819
$anyDataSet = new XmlDocument("<anydataset></anydataset>");
1920
foreach ($collection as $sr) {
@@ -25,9 +26,9 @@ protected function anydatasetXml($collection)
2526

2627
/**
2728
* @param array $row
28-
* @return XmlDocument
29+
* @return XmlNode
2930
*/
30-
protected function rowXml($row, XmlDocument $parentDocument = null)
31+
protected function rowXml(array $row, XmlDocument $parentDocument = null): XmlNode
3132
{
3233
if (!empty($parentDocument)) {
3334
$node = $parentDocument->appendChild('row');
@@ -47,7 +48,7 @@ protected function rowXml($row, XmlDocument $parentDocument = null)
4748
/**
4849
* @inheritDoc
4950
*/
50-
public function raw()
51+
public function raw(): mixed
5152
{
5253
if ($this->object instanceof GenericIterator) {
5354
return $this->anydatasetXml($this->object->toArray())->DOMDocument();
@@ -58,7 +59,7 @@ public function raw()
5859
/**
5960
* @inheritDoc
6061
*/
61-
public function toText()
62+
public function toText(): string
6263
{
6364
return $this->raw()->saveXML();
6465
}

0 commit comments

Comments
 (0)