Skip to content

Add type declarations #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require-dev": {
"kint-php/kint": "^3.3",
"phpunit/phpunit": "*",
"friendsofphp/php-cs-fixer": "^3.39"
"friendsofphp/php-cs-fixer": "^3.39",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 7 additions & 4 deletions examples/CustomFlowElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
*
*/

// Uncomment the line below when running the example as a standalone script
// require_once __DIR__ . '/../vendor/autoload.php';

use fiftyone\pipeline\core\BasicListEvidenceKeyFilter;
use fiftyone\pipeline\core\ElementDataDictionary;
use fiftyone\pipeline\core\FlowElement;
Expand Down Expand Up @@ -84,9 +87,9 @@ class AstrologyFlowElement extends FlowElement
{
// datakey used to categorise data coming back from this
// FlowElement in a Pipeline
public $dataKey = 'astrology';
public string $dataKey = 'astrology';

public $properties = [
public array $properties = [
'starSign' => [
'type' => 'string',
'description' => "the user's starsign"
Expand All @@ -103,7 +106,7 @@ class AstrologyFlowElement extends FlowElement

// The processInternal function is the core working of a FlowElement.
// It takes FlowData, reads evidence and returns data.
public function processInternal($flowData)
public function processInternal($flowData): void
{
$result = [];

Expand Down Expand Up @@ -140,7 +143,7 @@ public function processInternal($flowData)
$flowData->setElementData($data);
}

public function getEvidenceKeyFilter()
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
{
// A filter (in this case a basic list) stating which evidence
// the FlowElement is interested in
Expand Down
29 changes: 15 additions & 14 deletions examples/FlowElementsForExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,52 @@

use fiftyone\pipeline\core\BasicListEvidenceKeyFilter;
use fiftyone\pipeline\core\ElementDataDictionary;
use fiftyone\pipeline\core\FlowData;
use fiftyone\pipeline\core\FlowElement;

// Two simple FlowElements

class ExampleFlowElementA extends FlowElement
{
public $dataKey = 'example1';
public string $dataKey = 'example1';

public $properties = [
public array $properties = [
'exampleProperty1' => [
'type' => 'int'
]
];

public function processInternal($flowData)
public function processInternal(FlowData $flowData): void
{
$data = new ElementDataDictionary($this, ['exampleProperty1' => 5]);

$flowData->setElementData($data);
}

public function getEvidenceKeyFilter()
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
{
return new BasicListEvidenceKeyFilter(['header.user-agent']);
}
}

class ExampleFlowElementB extends FlowElement
{
public $dataKey = 'example2';
public string $dataKey = 'example2';

public $properties = [
public array $properties = [
'exampleProperty2' => [
'type' => 'int'
]
];

public function processInternal($flowData)
public function processInternal(FlowData $flowData): void
{
$data = new ElementDataDictionary($this, ['exampleProperty2' => 7]);

$flowData->setElementData($data);
}

public function getEvidenceKeyFilter()
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
{
return new BasicListEvidenceKeyFilter(['header.user-agent']);
}
Expand All @@ -78,14 +79,14 @@ public function getEvidenceKeyFilter()

class ErrorFlowElement extends FlowElement
{
public $dataKey = 'error';
public string $dataKey = 'error';

public function processInternal($flowData)
public function processInternal(FlowData $flowData): void
{
throw new Exception('Something went wrong');
}

public function getEvidenceKeyFilter()
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
{
return new BasicListEvidenceKeyFilter(['header.user-agent']);
}
Expand All @@ -97,14 +98,14 @@ public function getEvidenceKeyFilter()

class StopFlowElement extends FlowElement
{
public $dataKey = 'stop';
public string $dataKey = 'stop';

public function processInternal($flowData)
public function processInternal(FlowData $flowData): void
{
$flowData->stop();
}

public function getEvidenceKeyFilter()
public function getEvidenceKeyFilter(): BasicListEvidenceKeyFilter
{
return new BasicListEvidenceKeyFilter(['header.user-agent']);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ArrayLogger extends Logger
{
public $log = [];

public function logInternal($log)
public function logInternal(array $log): void
{
if ($log['message'] === 'test') {
$this->log[] = $log;
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
phpVersion: 70400
level: 6
paths:
- src
27 changes: 20 additions & 7 deletions src/AspectPropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

declare(strict_types=1);

namespace fiftyone\pipeline\core;

/**
* An AspectPropertyValue is a wrapper for a value
* It lets you check this wrapper has a value inside it
* If not value is set, a specific no value message is returned.
*
* @property mixed $value
*/
class AspectPropertyValue
{
public $noValueMessage;
public $hasValue = false;
public ?string $noValueMessage = null;
public bool $hasValue = false;

/**
* @var mixed
*/
private $_value;

/**
Expand All @@ -40,15 +48,15 @@ class AspectPropertyValue
* @param null|string $noValueMessage Reason why the value is missing
* @param mixed $value
*/
public function __construct($noValueMessage = null, $value = 'noValue')
public function __construct(?string $noValueMessage = null, $value = 'noValue')
{
if ($value !== 'noValue') {
$this->value = $value;
$this->noValueMessage = null;
$this->hasValue = true;
}

if ($noValueMessage) {
if (!empty($noValueMessage)) {
$this->hasValue = false;
$this->noValueMessage = $noValueMessage;
}
Expand All @@ -57,11 +65,10 @@ public function __construct($noValueMessage = null, $value = 'noValue')
/**
* Magic getter to access the value or throw an error with the no value message.
*
* @param string $key
* @return mixed
* @throws \Exception
*/
public function __get($key)
public function __get(string $key)
{
if ($key === 'value') {
if ($this->hasValue) {
Expand All @@ -71,9 +78,15 @@ public function __get($key)
throw new \Exception($this->noValueMessage);
}
}

return null;
}

public function __set($key, $value)
/**
* @param mixed $value
* @return void
*/
public function __set(string $key, $value)
{
if ($key === 'value') {
$this->_value = $value;
Expand Down
17 changes: 11 additions & 6 deletions src/BasicListEvidenceKeyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

declare(strict_types=1);

namespace fiftyone\pipeline\core;

/**
Expand All @@ -29,12 +31,15 @@
*/
class BasicListEvidenceKeyFilter extends EvidenceKeyFilter
{
private $list;
/**
* @var array<string>
*/
private array $list;

/**
* @param array $list an array of keys to keep
* @param array<string> $list An array of keys to keep
*/
public function __construct($list)
public function __construct(array $list)
{
$this->list = $list;
}
Expand All @@ -43,7 +48,7 @@ public function __construct($list)
* @param string $key key to check in the filter
* @return bool is this key in the filter's keys list?
*/
public function filterEvidenceKey($key)
public function filterEvidenceKey(string $key): bool
{
$key = strtolower($key);

Expand All @@ -59,9 +64,9 @@ public function filterEvidenceKey($key)
/**
* Get the internal list of evidence keys in this filter.
*
* @return array evidence keys
* @return array<string> evidence keys
*/
public function getList()
public function getList(): array
{
return $this->list;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

declare(strict_types=1);

namespace fiftyone\pipeline\core;

/**
Expand Down
43 changes: 17 additions & 26 deletions src/ElementData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

declare(strict_types=1);

namespace fiftyone\pipeline\core;

/**
Expand All @@ -29,19 +31,17 @@
*/
class ElementData
{
public $flowElement;
public FlowElement $flowElement;

/**
* Constructor for element data.
*
* @param FlowElement $flowElement
*/
public function __construct($flowElement)
public function __construct(FlowElement $flowElement)
{
$this->flowElement = $flowElement;
}

public function __get($key)
/**
* @return mixed
*/
public function __get(string $key)
{
return $this->get($key);
}
Expand All @@ -53,7 +53,7 @@ public function __get($key)
* @param string $key Property name
* @return mixed
*/
public function get($key)
public function get(string $key)
{
return $this->getInternal($key);
}
Expand All @@ -62,42 +62,33 @@ public function get($key)
* Get the values contained in the ElementData instance as a dictionary
* of keys and values.
*
* @return array
* @return array<string, mixed>
*/
public function asDictionary()
public function asDictionary(): array
{
return;
return [];
}

/**
* Helper method to get property as a string.
*
* @param string $key Property name
* @return string
*/
public function getAsString($key)
public function getAsString(string $key): string
{
return strval($this->get($key));
}

/**
* Helper method to get property as a float.
*
* @param string $key
* @return float
*/
public function getAsFloat($key)
public function getAsFloat(string $key): float
{
return floatval($this->get($key));
}

/**
* Helper method to get property as a int.
*
* @param string $key Property name
* @return int
* Helper method to get property as an int.
*/
public function getAsInteger($key)
public function getAsInteger(string $key): int
{
return intval($this->get($key));
}
Expand All @@ -109,7 +100,7 @@ public function getAsInteger($key)
* @param string $key Property name
* @return mixed
*/
protected function getInternal($key)
protected function getInternal(string $key)
{
return;
}
Expand Down
Loading