Skip to content

Commit

Permalink
add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
fiedsch committed Feb 4, 2023
1 parent 1458577 commit 887ec38
Show file tree
Hide file tree
Showing 27 changed files with 326 additions and 331 deletions.
63 changes: 32 additions & 31 deletions src/Fiedsch/Data/Augmentation/Augmentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Fiedsch\Data\Augmentation;

use Pimple\Container;
use RuntimeException;

/**
* Class Augmentor
Expand Down Expand Up @@ -40,7 +41,7 @@ class Augmentor extends Container
*
* @param array $values
*/
public function __construct($values = [])
public function __construct(array $values = [])
{
parent::__construct($values);
}
Expand All @@ -53,19 +54,19 @@ public function __construct($values = [])
*
* @return array the original data (left unchanged) and the augmented data.
*/
public function augment($data)
public function augment(array $data): array
{
// initialize
$this[self::KEY_AUGMENTED] = [];
// get rules
$rulekeys = array_filter($this->keys(), function($key) {
return strpos($key, self::PREFIX_RULE) === 0;
return str_starts_with($key, self::PREFIX_RULE);
});
// apply rules
foreach ($rulekeys as $rulename) {
$augmentation_step = $this[$rulename]($this, $data);
if (!is_array($augmentation_step)) {
throw new \RuntimeException("augmentaion rule '$rulename' did not produce data. Make sure to return the array of augmented data.");
throw new RuntimeException("augmentation rule '$rulename' did not produce data. Make sure to return the array of augmented data.");
}
// make the augmented data so far available to the next rule
$this[self::KEY_AUGMENTED] = array_merge($this[self::KEY_AUGMENTED], $augmentation_step);
Expand All @@ -84,46 +85,46 @@ public function augment($data)
}

/**
* Check if all of the required columns (fields) have been set during the augmentaion steps.
* Check if the required columns (fields) have been set during the augmentation steps.
* Throw an exception if a column is missing.
* Also check if additional columns (not specified in the required columns) are present.
* This will also throw an exception.
*
* @throws \RuntimeException
* @throws RuntimeException
*/
protected function checkAugmented()
protected function checkAugmented(): void
{
if ($this->hasRequiredColumnsSpecification()) {
foreach ($this[self::KEY_REQUIRED_COLNAMES] as $key) {
if (!array_key_exists($key, $this[self::KEY_AUGMENTED])) {
throw new \RuntimeException("required column '$key' does not exist in augmented data'");
throw new RuntimeException("required column '$key' does not exist in augmented data'");
}
}
// Additionally check if we have data for columns not specified in $this[self::KEY_REQUIRED_COLNAMES]
$key_mismatch = array_diff(array_keys($this[self::KEY_AUGMENTED]), $this[self::KEY_REQUIRED_COLNAMES]);
if (!empty($key_mismatch)) {
throw new \RuntimeException("found keys not specified as required field: " . json_encode(array_values($key_mismatch)));
throw new RuntimeException("found keys not specified as required field: " . json_encode(array_values($key_mismatch)));
}
}

if ($this->hasColumnOrderSpecification()) {
// Side effect(?): specifying column order has the same effect as setting required columns (see above).
foreach ($this[self::KEY_COLOUMN_ORDER] as $key) {
if (!array_key_exists($key, $this[self::KEY_AUGMENTED])) {
throw new \RuntimeException("required column '$key' does not exist in augmented data'");
throw new RuntimeException("required column '$key' does not exist in augmented data'");
}
}
// Check if we have data for columns not specified in $this[self::KEY_COLOUMN_ORDER]
$key_mismatch = array_diff(array_keys($this[self::KEY_AUGMENTED]), $this[self::KEY_COLOUMN_ORDER]);
if (!empty($key_mismatch)) {
throw new \RuntimeException("found keys not specified in column order: " . json_encode(array_values($key_mismatch)));
throw new RuntimeException("found keys not specified in column order: " . json_encode(array_values($key_mismatch)));
}
}

if ($this->hasRequiredColumnsSpecification() && $this->hasColumnOrderSpecification()) {
// check if bot specifications do not contradict
if (array_diff($this[self::KEY_COLOUMN_ORDER], $this[self::KEY_REQUIRED_COLNAMES])) {
throw new \RuntimeException("specification mismatch required columns and column order do not match");
throw new RuntimeException("specification mismatch required columns and column order do not match");
}
}
}
Expand All @@ -133,15 +134,15 @@ protected function checkAugmented()
*
* @param array $colnames the names of the columns that have to be set during the augmentation steps.
*/
public function setRequiredColumns(array $colnames)
public function setRequiredColumns(array $colnames): void
{
$this[self::KEY_REQUIRED_COLNAMES] = $colnames;
}

/**
* @return array
*/
public function getRequiredColumns()
public function getRequiredColumns(): array
{
return $this[self::KEY_REQUIRED_COLNAMES];
}
Expand All @@ -155,15 +156,12 @@ public function getRequiredColumns()
*
* @param array $colnames determines the order of the column output.
*/
public function setColumnOutputOrder(array $colnames)
public function setColumnOutputOrder(array $colnames): void
{
$this[self::KEY_COLOUMN_ORDER] = $colnames;
}

/**
* @return array
*/
public function getColumnOutputOrder()
public function getColumnOutputOrder(): array
{
return $this[self::KEY_COLOUMN_ORDER];
}
Expand All @@ -173,16 +171,18 @@ public function getColumnOutputOrder()
*
* @return boolean
*/
public function hasRequiredColumnsSpecification() {
public function hasRequiredColumnsSpecification(): bool
{
return $this->offsetExists(self::KEY_REQUIRED_COLNAMES) && is_array($this[self::KEY_REQUIRED_COLNAMES]);
}

/**
* Do we have the a specification for the order in which the generated columns have to be output?
* Do we have the specification for the order in which the generated columns have to be output?
*
* @return boolean
* @return bool
*/
public function hasColumnOrderSpecification() {
public function hasColumnOrderSpecification(): bool
{
return $this->offsetExists(self::KEY_COLOUMN_ORDER) && is_array($this[self::KEY_COLOUMN_ORDER]);
}

Expand All @@ -192,7 +192,7 @@ public function hasColumnOrderSpecification() {
* @return array the augmented data so far (or an empty array, should this be called in the
* first augmentation step).
*/
public function getAugmentedSoFar()
public function getAugmentedSoFar(): array
{
if (!$this->offsetExists(self::KEY_AUGMENTED)) {
$this[self::KEY_AUGMENTED] = [];
Expand All @@ -205,12 +205,12 @@ public function getAugmentedSoFar()
*
* @param string $name the name of the augmentation rule
* @param callable $rule the code that will be executed
* @throws \RuntimeException
* @throws RuntimeException
*/
public function addRule($name, callable $rule)
public function addRule(string $name, callable $rule): void
{
if (isset($this[self::rule($name)])) {
throw new \RuntimeException("rule '$name' already exists'");
throw new RuntimeException("rule '$name' already exists'");
}
$this[self::rule($name)] = $this->protect($rule);
}
Expand All @@ -223,7 +223,7 @@ public function addRule($name, callable $rule)
*
* @return string key used to store the rule
*/
protected static function rule($name)
protected static function rule(string $name): string
{
return self::PREFIX_RULE . $name;
}
Expand All @@ -243,11 +243,11 @@ protected static function rule($name)
* See also: https://github.com/silexphp/Pimple/issues/149
* <quote>
* fabpot commented on Jul 15, 2014
* To be more precise, Pimple stores parameters but it should have
* To be more precise, Pimple stores parameters, but it should have
* no knowledge of the parameter value; Pimple just stores what you give it.
* </quote>
*/
public function appendTo($key, $value)
public function appendTo(string $key, mixed $value): void
{
if (!$this->offsetExists($key)) {
$this[$key] = [$value];
Expand All @@ -271,7 +271,8 @@ public function appendTo($key, $value)
* @param $key
* @param $value
*/
public function overwriteValue($key, $value) {
public function overwriteValue($key, $value): void
{
$augmented = $this[self::KEY_AUGMENTED];
$augmented[$key] = $value;
$this[self::KEY_AUGMENTED] = $augmented;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class QuotaCellServiceProvider implements ServiceProviderInterface

/**
* @param Container $container the dependency injection container.
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
* @noinspection PhpUnused
*/
public function register(Container $container)
public function register(Container $container): void
{
if (!$container->offsetExists('quota.targets')) {
$container['quota.targets'] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class TokenServiceProvider implements ServiceProviderInterface

/**
* @param Container $container the dependency injection container.
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
* @noinspection PhpUnused
*/
public function register(Container $container)
public function register(Container $container): void
{

if (!$container->offsetExists('token.length')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class UniquenessCheckerServiceProvider implements ServiceProviderInterface

/**
* @param Container $container the dependency injection container.
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
* @noinspection PhpUnused
*/
public function register(Container $container)
public function register(Container $container): void
{

$container['unique'] = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class ValidationServiceProvider implements ServiceProviderInterface

/**
* @param Container $container the dependency injection container.
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
* @noinspection PhpUnused
*/
public function register(Container $container)
public function register(Container $container): void
{

$container['validation'] = function () {
Expand Down
4 changes: 2 additions & 2 deletions src/Fiedsch/Data/Augmentation/Rules/AugmentationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Marker interface
* Objects implementing __invoke can be used as callables (since PHP 5.3)
* see Augmantor::augment() and Augmentor::addRule()
* see Augmentor::augment() and Augmentor::addRule()
*/

namespace Fiedsch\Data\Augmentation\Rules;
Expand All @@ -24,5 +24,5 @@ interface AugmentationRule
*
* @return array
*/
public function __invoke(Augmentor $augmentor, $data = null);
public function __invoke(Augmentor $augmentor, array $data): array;
}
2 changes: 1 addition & 1 deletion src/Fiedsch/Data/Augmentation/Rules/TokenRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TokenRule implements AugmentationRule
*
* @return array
*/
public function __invoke(Augmentor $augmentor, $data = null)
public function __invoke(Augmentor $augmentor, array $data): array
{
return ['token' => $augmentor['token']->getUniqueToken()];
}
Expand Down
Loading

0 comments on commit 887ec38

Please sign in to comment.