Skip to content

Commit a78d9ad

Browse files
Merged Pull Request '#34 types->main : Add type declarations'
Add type declarations
2 parents 8dc485e + f73b3e2 commit a78d9ad

32 files changed

+407
-348
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require-dev": {
2020
"kint-php/kint": "^3.3",
2121
"phpunit/phpunit": "*",
22-
"friendsofphp/php-cs-fixer": "^3.39"
22+
"friendsofphp/php-cs-fixer": "^3.39",
23+
"phpstan/phpstan": "^1.10"
2324
},
2425
"autoload": {
2526
"psr-4": {

examples/CustomFlowElement.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
*
3535
*/
3636

37+
// Uncomment the line below when running the example as a standalone script
38+
// require_once __DIR__ . '/../vendor/autoload.php';
39+
3740
use fiftyone\pipeline\core\BasicListEvidenceKeyFilter;
3841
use fiftyone\pipeline\core\ElementDataDictionary;
3942
use fiftyone\pipeline\core\FlowElement;
@@ -84,9 +87,9 @@ class AstrologyFlowElement extends FlowElement
8487
{
8588
// datakey used to categorise data coming back from this
8689
// FlowElement in a Pipeline
87-
public $dataKey = 'astrology';
90+
public string $dataKey = 'astrology';
8891

89-
public $properties = [
92+
public array $properties = [
9093
'starSign' => [
9194
'type' => 'string',
9295
'description' => "the user's starsign"
@@ -103,7 +106,7 @@ class AstrologyFlowElement extends FlowElement
103106

104107
// The processInternal function is the core working of a FlowElement.
105108
// It takes FlowData, reads evidence and returns data.
106-
public function processInternal($flowData)
109+
public function processInternal($flowData): void
107110
{
108111
$result = [];
109112

@@ -140,7 +143,7 @@ public function processInternal($flowData)
140143
$flowData->setElementData($data);
141144
}
142145

143-
public function getEvidenceKeyFilter()
146+
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
144147
{
145148
// A filter (in this case a basic list) stating which evidence
146149
// the FlowElement is interested in

examples/FlowElementsForExamples.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,52 @@
2323

2424
use fiftyone\pipeline\core\BasicListEvidenceKeyFilter;
2525
use fiftyone\pipeline\core\ElementDataDictionary;
26+
use fiftyone\pipeline\core\FlowData;
2627
use fiftyone\pipeline\core\FlowElement;
2728

2829
// Two simple FlowElements
2930

3031
class ExampleFlowElementA extends FlowElement
3132
{
32-
public $dataKey = 'example1';
33+
public string $dataKey = 'example1';
3334

34-
public $properties = [
35+
public array $properties = [
3536
'exampleProperty1' => [
3637
'type' => 'int'
3738
]
3839
];
3940

40-
public function processInternal($flowData)
41+
public function processInternal(FlowData $flowData): void
4142
{
4243
$data = new ElementDataDictionary($this, ['exampleProperty1' => 5]);
4344

4445
$flowData->setElementData($data);
4546
}
4647

47-
public function getEvidenceKeyFilter()
48+
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
4849
{
4950
return new BasicListEvidenceKeyFilter(['header.user-agent']);
5051
}
5152
}
5253

5354
class ExampleFlowElementB extends FlowElement
5455
{
55-
public $dataKey = 'example2';
56+
public string $dataKey = 'example2';
5657

57-
public $properties = [
58+
public array $properties = [
5859
'exampleProperty2' => [
5960
'type' => 'int'
6061
]
6162
];
6263

63-
public function processInternal($flowData)
64+
public function processInternal(FlowData $flowData): void
6465
{
6566
$data = new ElementDataDictionary($this, ['exampleProperty2' => 7]);
6667

6768
$flowData->setElementData($data);
6869
}
6970

70-
public function getEvidenceKeyFilter()
71+
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
7172
{
7273
return new BasicListEvidenceKeyFilter(['header.user-agent']);
7374
}
@@ -78,14 +79,14 @@ public function getEvidenceKeyFilter()
7879

7980
class ErrorFlowElement extends FlowElement
8081
{
81-
public $dataKey = 'error';
82+
public string $dataKey = 'error';
8283

83-
public function processInternal($flowData)
84+
public function processInternal(FlowData $flowData): void
8485
{
8586
throw new Exception('Something went wrong');
8687
}
8788

88-
public function getEvidenceKeyFilter()
89+
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
8990
{
9091
return new BasicListEvidenceKeyFilter(['header.user-agent']);
9192
}
@@ -97,14 +98,14 @@ public function getEvidenceKeyFilter()
9798

9899
class StopFlowElement extends FlowElement
99100
{
100-
public $dataKey = 'stop';
101+
public string $dataKey = 'stop';
101102

102-
public function processInternal($flowData)
103+
public function processInternal(FlowData $flowData): void
103104
{
104105
$flowData->stop();
105106
}
106107

107-
public function getEvidenceKeyFilter()
108+
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
108109
{
109110
return new BasicListEvidenceKeyFilter(['header.user-agent']);
110111
}

examples/Pipeline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ArrayLogger extends Logger
4949
{
5050
public $log = [];
5151

52-
public function logInternal($log)
52+
public function logInternal(array $log): void
5353
{
5454
if ($log['message'] === 'test') {
5555
$this->log[] = $log;

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
phpVersion: 70400
3+
level: 6
4+
paths:
5+
- src

src/AspectPropertyValue.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,25 @@
2121
* such notice(s) shall fulfill the requirements of that article.
2222
* ********************************************************************* */
2323

24+
declare(strict_types=1);
25+
2426
namespace fiftyone\pipeline\core;
2527

2628
/**
2729
* An AspectPropertyValue is a wrapper for a value
2830
* It lets you check this wrapper has a value inside it
2931
* If not value is set, a specific no value message is returned.
32+
*
33+
* @property mixed $value
3034
*/
3135
class AspectPropertyValue
3236
{
33-
public $noValueMessage;
34-
public $hasValue = false;
37+
public ?string $noValueMessage = null;
38+
public bool $hasValue = false;
39+
40+
/**
41+
* @var mixed
42+
*/
3543
private $_value;
3644

3745
/**
@@ -40,15 +48,15 @@ class AspectPropertyValue
4048
* @param null|string $noValueMessage Reason why the value is missing
4149
* @param mixed $value
4250
*/
43-
public function __construct($noValueMessage = null, $value = 'noValue')
51+
public function __construct(?string $noValueMessage = null, $value = 'noValue')
4452
{
4553
if ($value !== 'noValue') {
4654
$this->value = $value;
4755
$this->noValueMessage = null;
4856
$this->hasValue = true;
4957
}
5058

51-
if ($noValueMessage) {
59+
if (!empty($noValueMessage)) {
5260
$this->hasValue = false;
5361
$this->noValueMessage = $noValueMessage;
5462
}
@@ -57,11 +65,10 @@ public function __construct($noValueMessage = null, $value = 'noValue')
5765
/**
5866
* Magic getter to access the value or throw an error with the no value message.
5967
*
60-
* @param string $key
6168
* @return mixed
6269
* @throws \Exception
6370
*/
64-
public function __get($key)
71+
public function __get(string $key)
6572
{
6673
if ($key === 'value') {
6774
if ($this->hasValue) {
@@ -71,9 +78,15 @@ public function __get($key)
7178
throw new \Exception($this->noValueMessage);
7279
}
7380
}
81+
82+
return null;
7483
}
7584

76-
public function __set($key, $value)
85+
/**
86+
* @param mixed $value
87+
* @return void
88+
*/
89+
public function __set(string $key, $value)
7790
{
7891
if ($key === 'value') {
7992
$this->_value = $value;

src/BasicListEvidenceKeyFilter.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* such notice(s) shall fulfill the requirements of that article.
2222
* ********************************************************************* */
2323

24+
declare(strict_types=1);
25+
2426
namespace fiftyone\pipeline\core;
2527

2628
/**
@@ -29,12 +31,15 @@
2931
*/
3032
class BasicListEvidenceKeyFilter extends EvidenceKeyFilter
3133
{
32-
private $list;
34+
/**
35+
* @var array<string>
36+
*/
37+
private array $list;
3338

3439
/**
35-
* @param array $list an array of keys to keep
40+
* @param array<string> $list An array of keys to keep
3641
*/
37-
public function __construct($list)
42+
public function __construct(array $list)
3843
{
3944
$this->list = $list;
4045
}
@@ -43,7 +48,7 @@ public function __construct($list)
4348
* @param string $key key to check in the filter
4449
* @return bool is this key in the filter's keys list?
4550
*/
46-
public function filterEvidenceKey($key)
51+
public function filterEvidenceKey(string $key): bool
4752
{
4853
$key = strtolower($key);
4954

@@ -59,9 +64,9 @@ public function filterEvidenceKey($key)
5964
/**
6065
* Get the internal list of evidence keys in this filter.
6166
*
62-
* @return array evidence keys
67+
* @return array<string> evidence keys
6368
*/
64-
public function getList()
69+
public function getList(): array
6570
{
6671
return $this->list;
6772
}

src/Constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* such notice(s) shall fulfill the requirements of that article.
2222
* ********************************************************************* */
2323

24+
declare(strict_types=1);
25+
2426
namespace fiftyone\pipeline\core;
2527

2628
/**

src/ElementData.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* such notice(s) shall fulfill the requirements of that article.
2222
* ********************************************************************* */
2323

24+
declare(strict_types=1);
25+
2426
namespace fiftyone\pipeline\core;
2527

2628
/**
@@ -29,19 +31,17 @@
2931
*/
3032
class ElementData
3133
{
32-
public $flowElement;
34+
public FlowElement $flowElement;
3335

34-
/**
35-
* Constructor for element data.
36-
*
37-
* @param FlowElement $flowElement
38-
*/
39-
public function __construct($flowElement)
36+
public function __construct(FlowElement $flowElement)
4037
{
4138
$this->flowElement = $flowElement;
4239
}
4340

44-
public function __get($key)
41+
/**
42+
* @return mixed
43+
*/
44+
public function __get(string $key)
4545
{
4646
return $this->get($key);
4747
}
@@ -53,7 +53,7 @@ public function __get($key)
5353
* @param string $key Property name
5454
* @return mixed
5555
*/
56-
public function get($key)
56+
public function get(string $key)
5757
{
5858
return $this->getInternal($key);
5959
}
@@ -62,42 +62,33 @@ public function get($key)
6262
* Get the values contained in the ElementData instance as a dictionary
6363
* of keys and values.
6464
*
65-
* @return array
65+
* @return array<string, mixed>
6666
*/
67-
public function asDictionary()
67+
public function asDictionary(): array
6868
{
69-
return;
69+
return [];
7070
}
7171

7272
/**
7373
* Helper method to get property as a string.
74-
*
75-
* @param string $key Property name
76-
* @return string
7774
*/
78-
public function getAsString($key)
75+
public function getAsString(string $key): string
7976
{
8077
return strval($this->get($key));
8178
}
8279

8380
/**
8481
* Helper method to get property as a float.
85-
*
86-
* @param string $key
87-
* @return float
8882
*/
89-
public function getAsFloat($key)
83+
public function getAsFloat(string $key): float
9084
{
9185
return floatval($this->get($key));
9286
}
9387

9488
/**
95-
* Helper method to get property as a int.
96-
*
97-
* @param string $key Property name
98-
* @return int
89+
* Helper method to get property as an int.
9990
*/
100-
public function getAsInteger($key)
91+
public function getAsInteger(string $key): int
10192
{
10293
return intval($this->get($key));
10394
}
@@ -109,7 +100,7 @@ public function getAsInteger($key)
109100
* @param string $key Property name
110101
* @return mixed
111102
*/
112-
protected function getInternal($key)
103+
protected function getInternal(string $key)
113104
{
114105
return;
115106
}

0 commit comments

Comments
 (0)