diff --git a/src/Analyser/PatternAnalyser.php b/src/Analyser/PatternAnalyser.php index 824526c..7b4a1d8 100644 --- a/src/Analyser/PatternAnalyser.php +++ b/src/Analyser/PatternAnalyser.php @@ -108,6 +108,7 @@ public function overridePossibleInsightClass(string $parentInsightClass, string public function analyse(): AnalysisInterface { $analysis = new Analysis(); + $analysis->setLog($this->log); foreach ($this->log as $entry) { foreach ($this->possibleInsightClasses as $possibleInsightClass) { diff --git a/src/Analysis/Analysis.php b/src/Analysis/Analysis.php index c2af2b0..fdfde23 100644 --- a/src/Analysis/Analysis.php +++ b/src/Analysis/Analysis.php @@ -2,6 +2,8 @@ namespace Aternos\Codex\Analysis; +use Aternos\Codex\Log\LogInterface; + /** * Class Analysis * @@ -14,6 +16,7 @@ class Analysis implements AnalysisInterface */ protected array $insights = []; protected int $iterator = 0; + protected ?LogInterface $log = null; /** * Set all insights at once in an array replacing the current insights @@ -23,6 +26,9 @@ class Analysis implements AnalysisInterface */ public function setInsights(array $insights = []): static { + foreach ($insights as $insight) { + $insight->setAnalysis($this); + } $this->insights = $insights; return $this; } @@ -43,6 +49,7 @@ public function addInsight(InsightInterface $insight): static } } + $insight->setAnalysis($this); $this->insights[] = $insight; return $this; } @@ -185,6 +192,7 @@ public function offsetGet(mixed $offset): InsightInterface */ public function offsetSet(mixed $offset, mixed $value): void { + $value->setAnalysis($this); $this->insights[$offset] = $value; } @@ -208,4 +216,22 @@ public function jsonSerialize(): array "information" => $this->getInformation() ]; } + + /** + * @param LogInterface $log + * @return $this + */ + public function setLog(LogInterface $log): static + { + $this->log = $log; + return $this; + } + + /** + * @return LogInterface|null + */ + public function getLog(): ?LogInterface + { + return $this->log; + } } \ No newline at end of file diff --git a/src/Analysis/AnalysisInterface.php b/src/Analysis/AnalysisInterface.php index 9376ace..eccaa0b 100644 --- a/src/Analysis/AnalysisInterface.php +++ b/src/Analysis/AnalysisInterface.php @@ -3,6 +3,8 @@ namespace Aternos\Codex\Analysis; use ArrayAccess; +use Aternos\Codex\Log\AnalysableLogInterface; +use Aternos\Codex\Log\LogInterface; use Countable; use Iterator; use JsonSerializable; @@ -14,6 +16,21 @@ */ interface AnalysisInterface extends Iterator, Countable, ArrayAccess, JsonSerializable { + /** + * Set the log + * + * @param LogInterface $log + * @return $this + */ + public function setLog(LogInterface $log): static; + + /** + * Get the log + * + * @return LogInterface|null + */ + public function getLog(): ?LogInterface; + /** * Set all insights at once in an array replacing the current insights * diff --git a/src/Analysis/Insight.php b/src/Analysis/Insight.php index 90d3282..c4f2bcc 100644 --- a/src/Analysis/Insight.php +++ b/src/Analysis/Insight.php @@ -3,6 +3,7 @@ namespace Aternos\Codex\Analysis; use Aternos\Codex\Log\EntryInterface; +use Aternos\Codex\Log\LogInterface; /** * Class Insight @@ -11,6 +12,7 @@ */ abstract class Insight implements InsightInterface { + protected ?AnalysisInterface $analysis = null; protected ?EntryInterface $entry = null; protected int $counter = 1; @@ -76,4 +78,42 @@ public function jsonSerialize(): array 'entry' => $this->getEntry() ]; } + + /** + * Set the related analysis + * + * @param AnalysisInterface $analysis + * @return $this + */ + public function setAnalysis(AnalysisInterface $analysis): static + { + $this->analysis = $analysis; + return $this; + } + + /** + * Get the related analysis + * + * @return AnalysisInterface|null + */ + public function getAnalysis(): ?AnalysisInterface + { + return $this->analysis; + } + + /** + * @return LogInterface|null + */ + protected function getLog(): ?LogInterface + { + return $this->getAnalysis()?->getLog(); + } + + /** + * @return string|null + */ + protected function getLogContent(): ?string + { + return $this->getLog()?->getLogFile()?->getContent(); + } } \ No newline at end of file diff --git a/src/Analysis/InsightInterface.php b/src/Analysis/InsightInterface.php index 3a9c9e9..5ee82b0 100644 --- a/src/Analysis/InsightInterface.php +++ b/src/Analysis/InsightInterface.php @@ -60,4 +60,19 @@ public function increaseCounter(): static; * @return int */ public function getCounterValue(): int; + + /** + * Set the related analysis + * + * @param AnalysisInterface $analysis + * @return $this + */ + public function setAnalysis(AnalysisInterface $analysis): static; + + /** + * Get the related analysis + * + * @return AnalysisInterface|null + */ + public function getAnalysis(): ?AnalysisInterface; } \ No newline at end of file diff --git a/test/tests/Analyser/PatternAnalyserTest.php b/test/tests/Analyser/PatternAnalyserTest.php index 9bd1382..ac62358 100644 --- a/test/tests/Analyser/PatternAnalyserTest.php +++ b/test/tests/Analyser/PatternAnalyserTest.php @@ -66,7 +66,7 @@ public function testAnalyse(): void $log->parse(); $analysis = $log->analyse(); - $this->assertEquals($this->getExpectedAnalysis()->getInsights(), $analysis->getInsights()); + $this->assertJsonStringEqualsJsonString(json_encode($this->getExpectedAnalysis()->getInsights()), json_encode($analysis->getInsights())); } public function testAnalyseWithPossibleInsightClasses(): void @@ -82,7 +82,7 @@ public function testAnalyseWithPossibleInsightClasses(): void ]); $analysis = $log->analyse($analyser); - $this->assertEquals($this->getExpectedAnalysis()->getInsights(), $analysis->getInsights()); + $this->assertJsonStringEqualsJsonString(json_encode($this->getExpectedAnalysis()->getInsights()), json_encode($analysis->getInsights())); } public function testAddPossibleInsightClassThrowsExceptionIfPossibleInsightClassDoesNotImplementPatternInsightInterface(): void