Skip to content

Commit

Permalink
Merge pull request #15 from aternosorg/2.0
Browse files Browse the repository at this point in the history
 Minimum PHP version: 8.1
 Correct return/argument/property types
 Entry level as enum
 JSON serializable analysis/insights
  • Loading branch information
matthi4s authored Aug 17, 2022
2 parents f015e29 + cee752f commit 18639f0
Show file tree
Hide file tree
Showing 67 changed files with 697 additions and 595 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php-version: [ '7.4', '8.0', '8.1' ]
php-version: [ '8.1' ]

name: Run tests on PHP v${{ matrix.php-version }}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=7.4"
"php": ">=8.1"
},
"autoload": {
"psr-4": {
Expand Down
254 changes: 88 additions & 166 deletions composer.lock

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/Analyser/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
*/
abstract class Analyser implements AnalyserInterface
{
/**
* @var AnalysableLogInterface
*/
protected $log;
protected ?AnalysableLogInterface $log = null;

/**
* Set the log
*
* @param AnalysableLogInterface $log
* @return $this
*/
public function setLog(AnalysableLogInterface $log)
public function setLog(AnalysableLogInterface $log): static
{
$this->log = $log;
return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/AnalyserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ interface AnalyserInterface
* @param AnalysableLogInterface $log
* @return $this
*/
public function setLog(AnalysableLogInterface $log);
public function setLog(AnalysableLogInterface $log): static;

/**
* Analyse a log and return an Analysis
*
* @return AnalysisInterface
*/
public function analyse();
public function analyse(): AnalysisInterface;
}
45 changes: 23 additions & 22 deletions src/Analyser/PatternAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Aternos\Codex\Analysis\AnalysisInterface;
use Aternos\Codex\Analysis\PatternInsightInterface;
use Aternos\Codex\Log\EntryInterface;
use InvalidArgumentException;

/**
* Class PatternAnalyser
Expand All @@ -15,19 +16,19 @@
class PatternAnalyser extends Analyser
{
/**
* @var array
* @var class-string<PatternInsightInterface>[]
*/
protected $possibleInsightClasses = [];
protected array $possibleInsightClasses = [];

/**
* Set possible insight classes
*
* Every class must implement PatternInsightInterface
*
* @param array $insightClasses
* @param class-string<PatternInsightInterface>[] $insightClasses
* @return $this
*/
public function setPossibleInsightClasses(array $insightClasses)
public function setPossibleInsightClasses(array $insightClasses): static
{
$this->possibleInsightClasses = [];
foreach ($insightClasses as $insightClass) {
Expand All @@ -42,13 +43,13 @@ public function setPossibleInsightClasses(array $insightClasses)
*
* The class must implement PatternInsightInterface
*
* @param string $insightClass
* @param class-string<PatternInsightInterface> $insightClass
* @return $this
*/
public function addPossibleInsightClass(string $insightClass)
public function addPossibleInsightClass(string $insightClass): static
{
if (!is_subclass_of($insightClass, PatternInsightInterface::class)) {
throw new \InvalidArgumentException("Class " . $insightClass . " does not implement " . PatternInsightInterface::class . ".");
throw new InvalidArgumentException("Class " . $insightClass . " does not implement " . PatternInsightInterface::class . ".");
}

$this->possibleInsightClasses[] = $insightClass;
Expand All @@ -58,24 +59,24 @@ public function addPossibleInsightClass(string $insightClass)
/**
* Find a possible insight class
*
* @param string $insightClass
* @return int|string
* @param class-string<PatternInsightInterface> $insightClass
* @return int
*/
protected function findPossibleInsightClass(string $insightClass)
protected function findPossibleInsightClass(string $insightClass): int
{
$index = array_search($insightClass, $this->possibleInsightClasses);
if ($index === false) {
throw new \InvalidArgumentException("Class " . $insightClass . " not found in possible insight classes.");
throw new InvalidArgumentException("Class " . $insightClass . " not found in possible insight classes.");
}
return $index;
}

/**
* Remove a possible insight class
*
* @param string $insightClass
* @param class-string<PatternInsightInterface> $insightClass
*/
public function removePossibleInsightClass(string $insightClass)
public function removePossibleInsightClass(string $insightClass): void
{
$index = $this->findPossibleInsightClass($insightClass);
unset($this->possibleInsightClasses[$index]);
Expand All @@ -86,13 +87,13 @@ public function removePossibleInsightClass(string $insightClass)
*
* The $childInsightClass has to extend $parentInsightClass
*
* @param string $parentInsightClass
* @param string $childInsightClass
* @param class-string<PatternInsightInterface> $parentInsightClass
* @param class-string<PatternInsightInterface> $childInsightClass
*/
public function overridePossibleInsightClass(string $parentInsightClass, string $childInsightClass)
public function overridePossibleInsightClass(string $parentInsightClass, string $childInsightClass): void
{
if (!is_subclass_of($childInsightClass, $parentInsightClass)) {
throw new \InvalidArgumentException("Class " . $childInsightClass . " does not extend " . $parentInsightClass . ".");
throw new InvalidArgumentException("Class " . $childInsightClass . " does not extend " . $parentInsightClass . ".");
}

$index = $this->findPossibleInsightClass($parentInsightClass);
Expand All @@ -104,7 +105,7 @@ public function overridePossibleInsightClass(string $parentInsightClass, string
*
* @return AnalysisInterface
*/
public function analyse()
public function analyse(): AnalysisInterface
{
$analysis = new Analysis();

Expand All @@ -131,15 +132,15 @@ public function analyse()
*
* @param EntryInterface $entry
* @param string $possibleInsightClass
* @param $patternKey
* @param mixed $patternKey
* @param string $pattern
* @return bool|PatternInsightInterface[]
* @return null|PatternInsightInterface[]
*/
protected function analyseEntry(EntryInterface $entry, string $possibleInsightClass, $patternKey, string $pattern)
protected function analyseEntry(EntryInterface $entry, string $possibleInsightClass, mixed $patternKey, string $pattern): ?array
{
$result = preg_match_all($pattern, $entry, $matches, PREG_SET_ORDER);
if ($result === false || $result === 0) {
return false;
return null;
}

$return = [];
Expand Down
47 changes: 27 additions & 20 deletions src/Analysis/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ class Analysis implements AnalysisInterface
/**
* @var InsightInterface[]
*/
protected $insights = [];

/**
* @var int
*/
protected $iterator = 0;
protected array $insights = [];
protected int $iterator = 0;

/**
* Set all insights at once in an array replacing the current insights
*
* @param InsightInterface[] $insights
* @return $this
*/
public function setInsights(array $insights = [])
public function setInsights(array $insights = []): static
{
$this->insights = $insights;
return $this;
Expand All @@ -38,7 +34,7 @@ public function setInsights(array $insights = [])
* @param InsightInterface $insight
* @return $this
*/
public function addInsight(InsightInterface $insight)
public function addInsight(InsightInterface $insight): static
{
foreach ($this as $existingInsight) {
if (get_class($insight) === get_class($existingInsight) && $existingInsight->isEqual($insight)) {
Expand All @@ -54,7 +50,7 @@ public function addInsight(InsightInterface $insight)
/**
* Get all insights
*
* @return array
* @return InsightInterface[]
*/
public function getInsights(): array
{
Expand All @@ -64,10 +60,10 @@ public function getInsights(): array
/**
* Get all insights that are extended from $extendedFrom (class name)
*
* @param string $extendedFrom
* @return array
* @param class-string<InsightInterface> $extendedFrom
* @return InsightInterface[]
*/
public function getFilteredInsights($extendedFrom)
public function getFilteredInsights(string $extendedFrom): array
{
$returnInsights = [];
foreach ($this->getInsights() as $insight) {
Expand All @@ -82,7 +78,7 @@ public function getFilteredInsights($extendedFrom)
/**
* Get all problem insights
*
* @return array
* @return ProblemInterface[]
*/
public function getProblems(): array
{
Expand All @@ -92,7 +88,7 @@ public function getProblems(): array
/**
* Get all information insights
*
* @return array
* @return InformationInterface[]
*/
public function getInformation(): array
{
Expand Down Expand Up @@ -165,7 +161,7 @@ public function count(): int
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return isset($this->insights[$offset]);
}
Expand All @@ -176,29 +172,40 @@ public function offsetExists($offset): bool
* @param mixed $offset
* @return InsightInterface
*/
public function offsetGet($offset): InsightInterface
public function offsetGet(mixed $offset): InsightInterface
{
return $this->insights[$offset];
}

/**
* Offset to set
*
* @param $offset
* @param mixed $offset
* @param InsightInterface $value
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
$this->insights[$offset] = $value;
}

/**
* Offset to unset
*
* @param $offset
* @param mixed $offset
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
unset($this->insights[$offset]);
}

/**
* @return array
*/
public function jsonSerialize(): array
{
return [
"problems" => $this->getProblems(),
"information" => $this->getInformation()
];
}
}
37 changes: 32 additions & 5 deletions src/Analysis/AnalysisInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,60 @@

namespace Aternos\Codex\Analysis;

use ArrayAccess;
use Countable;
use Iterator;
use JsonSerializable;

/**
* Interface AnalysisInterface
*
* @package Aternos\Codex\Analysis
*/
interface AnalysisInterface extends \Iterator, \Countable, \ArrayAccess
interface AnalysisInterface extends Iterator, Countable, ArrayAccess, JsonSerializable
{
/**
* Set all insights at once in an array replacing the current insights
*
* @param array $insights
* @param InsightInterface[] $insights
* @return $this
*/
public function setInsights(array $insights = []);
public function setInsights(array $insights = []): static;

/**
* Add an insight
*
* @param InsightInterface $insight
* @return $this
*/
public function addInsight(InsightInterface $insight);
public function addInsight(InsightInterface $insight): static;

/**
* Get all insights
*
* @return array
* @return InsightInterface[]
*/
public function getInsights(): array;

/**
* Get all problem insights
*
* @return ProblemInterface[]
*/
public function getProblems(): array;

/**
* Get all information insights
*
* @return InformationInterface[]
*/
public function getInformation(): array;

/**
* Get all insights that are extended from $extendedFrom (class name)
*
* @param class-string<InsightInterface> $extendedFrom
* @return InsightInterface[]
*/
public function getFilteredInsights(string $extendedFrom): array;
}
Loading

0 comments on commit 18639f0

Please sign in to comment.