Skip to content

Commit

Permalink
added log to analysis, added analysis to insight
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Sep 30, 2022
1 parent 9ee1927 commit 8e22065
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Analyser/PatternAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions src/Analysis/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Aternos\Codex\Analysis;

use Aternos\Codex\Log\LogInterface;

/**
* Class Analysis
*
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -43,6 +49,7 @@ public function addInsight(InsightInterface $insight): static
}
}

$insight->setAnalysis($this);
$this->insights[] = $insight;
return $this;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
17 changes: 17 additions & 0 deletions src/Analysis/AnalysisInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down
40 changes: 40 additions & 0 deletions src/Analysis/Insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aternos\Codex\Analysis;

use Aternos\Codex\Log\EntryInterface;
use Aternos\Codex\Log\LogInterface;

/**
* Class Insight
Expand All @@ -11,6 +12,7 @@
*/
abstract class Insight implements InsightInterface
{
protected ?AnalysisInterface $analysis = null;
protected ?EntryInterface $entry = null;
protected int $counter = 1;

Expand Down Expand Up @@ -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();
}
}
15 changes: 15 additions & 0 deletions src/Analysis/InsightInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions test/tests/Analyser/PatternAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8e22065

Please sign in to comment.