Skip to content

Commit

Permalink
Add function name in tombstone #5
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Aug 11, 2020
1 parent 15eb622 commit 299b924
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/analyzer/Source/TombstoneExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public function extractTombstones(FilePathInterface $filePath): void
$this->currentFile = null;
}

public function onTombstoneFound(array $arguments, int $line, ?string $method): void
public function onTombstoneFound(string $functionName, array $arguments, int $line, ?string $method): void
{
if (null === $this->currentFile) {
throw new \RuntimeException('Current file not available.');
}

$tombstone = new Tombstone($arguments, $this->currentFile, $line, $method);
$tombstone = new Tombstone($functionName, $arguments, $this->currentFile, $line, $method);
$this->tombstoneIndex->addTombstone($tombstone);
}
}
2 changes: 1 addition & 1 deletion src/analyzer/Source/TombstoneVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function visitFunctionCallNode(FuncCall $node): void
$methodName = $this->getCurrentMethodName();
$arguments = $this->extractArguments($node);
/** @psalm-suppress PossiblyNullArgument */
$this->tombstoneCallback->onTombstoneFound($arguments, $line, $methodName);
$this->tombstoneCallback->onTombstoneFound('tombstone', $arguments, $line, $methodName);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/Format/AnalyzerLogFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AnalyzerLogFormat
{
private const CURRENT_VERSION = 10000; // 1.0.0
private const FIELD_VERSION = 'v';
private const FIELD_FUNCTION_NAME = 'fn';
private const FIELD_ARGUMENTS = 'a';
private const FIELD_FILE = 'f';
private const FIELD_LINE = 'l';
Expand All @@ -28,6 +29,7 @@ class AnalyzerLogFormat
];
private const REQUIRED_FIELDS_LOG = [
self::FIELD_INVOCATION_DATE,
self::FIELD_FUNCTION_NAME,
self::FIELD_ARGUMENTS,
self::FIELD_FILE,
self::FIELD_LINE,
Expand All @@ -37,6 +39,7 @@ public static function vampireToLog(Vampire $vampire): string
{
return json_encode([
self::FIELD_VERSION => self::CURRENT_VERSION,
self::FIELD_FUNCTION_NAME => $vampire->getFunctionName(),
self::FIELD_ARGUMENTS => $vampire->getArguments(),
self::FIELD_FILE => $vampire->getFile()->getReferencePath(),
self::FIELD_LINE => $vampire->getLine(),
Expand Down Expand Up @@ -84,6 +87,7 @@ public static function logToVampire(string $log, RootPath $rootDir): Vampire
$data[self::FIELD_INVOKER] ?? null,
self::decodeStackTrace($data[self::FIELD_STACKTRACE] ?? [], $rootDir),
new Tombstone(
$data[self::FIELD_FUNCTION_NAME],
$data[self::FIELD_ARGUMENTS],
$rootDir->createFilePath($data[self::FIELD_FILE]),
$data[self::FIELD_LINE],
Expand Down
19 changes: 15 additions & 4 deletions src/core/Model/Tombstone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class Tombstone
{
/**
* @var string
*/
private $functionName;

/**
* @var array
* @psalm-type list<string|null>
Expand Down Expand Up @@ -37,8 +42,9 @@ class Tombstone
*/
private $vampires = [];

public function __construct(array $arguments, FilePathInterface $file, int $line, ?string $method)
public function __construct(string $functionName, array $arguments, FilePathInterface $file, int $line, ?string $method)
{
$this->functionName = $functionName;
$this->arguments = $arguments;
$this->tombstoneDate = $this->findDate($arguments);
$this->file = $file;
Expand All @@ -53,17 +59,17 @@ public function __toString(): string
$argumentsList = '"'.implode('", "', $this->arguments).'"';
}

return 'tombstone('.$argumentsList.')';
return $this->functionName.'('.$argumentsList.')';
}

public function getHash(): int
{
return crc32($this->file->getReferencePath()."\n".$this->line."\n".implode(',', $this->arguments));
return crc32($this->file->getReferencePath()."\n".$this->line."\n".$this->functionName."\n".implode(',', $this->arguments));
}

public function inscriptionEquals(Tombstone $tombstone): bool
{
return $tombstone->getArguments() === $this->arguments;
return $tombstone->getFunctionName() === $this->functionName && $tombstone->getArguments() === $this->arguments;
}

private function findDate(array $arguments): ?string
Expand All @@ -77,6 +83,11 @@ private function findDate(array $arguments): ?string
return null;
}

public function getFunctionName(): string
{
return $this->functionName;
}

public function getArguments(): array
{
return $this->arguments;
Expand Down
5 changes: 5 additions & 0 deletions src/core/Model/Vampire.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function getTombstone(): Tombstone
return $this->tombstone;
}

public function getFunctionName(): string
{
return $this->tombstone->getFunctionName();
}

public function getArguments(): array
{
return $this->tombstone->getArguments();
Expand Down
4 changes: 2 additions & 2 deletions src/logger/Graveyard/BufferedGraveyard.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public function setAutoFlush(bool $autoFlush): void
$this->autoFlush = $autoFlush;
}

public function tombstone(array $arguments, array $trace, array $metadata): void
public function tombstone(string $functionName, array $arguments, array $trace, array $metadata): void
{
if ($this->autoFlush) {
$this->graveyard->tombstone($arguments, $trace, $metadata);
$this->graveyard->tombstone($functionName, $arguments, $trace, $metadata);
} else {
$this->tombstoneCalls[] = \func_get_args();
}
Expand Down
4 changes: 2 additions & 2 deletions src/logger/Graveyard/Graveyard.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public function __construct(VampireFactory $vampireFactory, ?LoggerInterface $lo
$this->handlers = $handlers;
}

public function tombstone(array $arguments, array $trace, array $metadata): void
public function tombstone(string $functionName, array $arguments, array $trace, array $metadata): void
{
try {
$vampire = $this->vampireFactory->createFromCall($arguments, $trace, $metadata);
$vampire = $this->vampireFactory->createFromCall($functionName, $arguments, $trace, $metadata);
foreach ($this->handlers as $handler) {
$handler->log($vampire);
}
Expand Down
2 changes: 1 addition & 1 deletion src/logger/Graveyard/GraveyardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface GraveyardInterface
{
public function tombstone(array $arguments, array $trace, array $metadata): void;
public function tombstone(string $functionName, array $arguments, array $trace, array $metadata): void;

public function flush(): void;
}
4 changes: 2 additions & 2 deletions src/logger/Graveyard/VampireFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(RootPath $rootPath, int $stackTraceDepth)
$this->stackTraceDepth = $stackTraceDepth;
}

public function createFromCall(array $arguments, array $trace, array $metadata): Vampire
public function createFromCall(string $functionName, array $arguments, array $trace, array $metadata): Vampire
{
// This is the call to the tombstone
$tombstoneCall = $trace[0];
Expand All @@ -47,7 +47,7 @@ public function createFromCall(array $arguments, array $trace, array $metadata):
$invoker = $this->getMethodFromFrame($trace[2]);
}

$tombstone = new Tombstone($arguments, $file, $line, $method);
$tombstone = new Tombstone($functionName, $arguments, $file, $line, $method);

$stackTrace = null;
if ($this->stackTraceDepth > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/logger/tombstone-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
function tombstone(string ...$arguments): void
{
$trace = \Scheb\Tombstone\Logger\Tracing\TraceProvider::getTraceHere();
\Scheb\Tombstone\Logger\Graveyard\GraveyardRegistry::getGraveyard()->tombstone($arguments, $trace, []);
\Scheb\Tombstone\Logger\Graveyard\GraveyardRegistry::getGraveyard()->tombstone(__FUNCTION__, $arguments, $trace, []);
}
}
4 changes: 2 additions & 2 deletions tests/Analyzer/Log/fixtures/allValid.tombstone
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"v":10000,"a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
{"v":10000,"a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
{"v":10000,"fn":"tombstone","a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
{"v":10000,"fn":"tombstone","a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
4 changes: 2 additions & 2 deletions tests/Analyzer/Log/fixtures/malformedData.tombstone
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"v":10000,"a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
{"v":10000,"fn":"tombstone","a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
malformed
{"v":10000,"a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
{"v":10000,"fn":"tombstone","a":["label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"\/path\/to\/file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}
12 changes: 6 additions & 6 deletions tests/Analyzer/Report/fixtures/AnalyzerResultFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class AnalyzerResultFixture
public static function getAnalyzerResult(): AnalyzerResult
{
$rootDir = new RootPath(__DIR__.'/source');
$functionTombstone = new Tombstone(['2020-01-01', 'globalFunction'], $rootDir->createFilePath('functions.php'), 7, 'globalFunction');
$globalScope = new Tombstone(['2020-01-01', 'globalScope'], $rootDir->createFilePath('functions.php'), 10, null);
$class1Tombstone = new Tombstone(['2020-01-01', 'Class1'], $rootDir->createFilePath('Class1.php'), 11, 'Foo\\Class1::staticMethod');
$class2Tombstone = new Tombstone(['2020-01-01', 'Class2'], $rootDir->createFilePath('Bar/Class2.php'), 11, 'Foo\\Bar\\Class2->publicMethod');
$class3Tombstone = new Tombstone(['2020-01-01', 'Class3'], $rootDir->createFilePath('Bar/Class3.php'), 11, 'Foo\\Bar\\Class3->someOtherMethod');
$deletedTombstone = new Tombstone(['2020-01-01', 'Class1'], $rootDir->createFilePath('Class1.php'), 18, 'Foo\\Class1->deletedMethod');
$functionTombstone = new Tombstone('tombstone', ['2020-01-01', 'globalFunction'], $rootDir->createFilePath('functions.php'), 7, 'globalFunction');
$globalScope = new Tombstone('tombstone', ['2020-01-01', 'globalScope'], $rootDir->createFilePath('functions.php'), 10, null);
$class1Tombstone = new Tombstone('tombstone', ['2020-01-01', 'Class1'], $rootDir->createFilePath('Class1.php'), 11, 'Foo\\Class1::staticMethod');
$class2Tombstone = new Tombstone('tombstone', ['2020-01-01', 'Class2'], $rootDir->createFilePath('Bar/Class2.php'), 11, 'Foo\\Bar\\Class2->publicMethod');
$class3Tombstone = new Tombstone('tombstone', ['2020-01-01', 'Class3'], $rootDir->createFilePath('Bar/Class3.php'), 11, 'Foo\\Bar\\Class3->someOtherMethod');
$deletedTombstone = new Tombstone('tombstone', ['2020-01-01', 'Class1'], $rootDir->createFilePath('Class1.php'), 18, 'Foo\\Class1->deletedMethod');

$vampire1 = new Vampire('2020-02-01', 'invoker1', new StackTrace(), $globalScope, []);
$globalScope->addVampire($vampire1);
Expand Down
2 changes: 1 addition & 1 deletion tests/Analyzer/Source/TombstoneExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function extractTombstones_tombstoneFound_addToTombstoneIndex(): void
->method('traverse')
->with($this->identicalTo($statements))
->willReturnCallback(function (array $statements): array {
$this->extractor->onTombstoneFound(['args'], 123, 'method');
$this->extractor->onTombstoneFound('tombstone', ['args'], 123, 'method');

return $statements;
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Format/AnalyzerLogFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class AnalyzerLogFormatTest extends TestCase
{
public const TOMBSTONE_ARGUMENTS = ['2014-01-01', 'label'];
public const LOG_RECORD = '{"v":10000,"a":["2014-01-01","label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}';
public const LOG_RECORD = '{"v":10000,"fn":"tombstone","a":["2014-01-01","label"],"f":"file","l":123,"m":"method","d":{"metaField":"metaValue"},"s":[{"f":"file1.php","l":11,"m":"ClassName->method"}],"id":"2015-01-01","im":"invoker"}';

/**
* @test
Expand Down Expand Up @@ -63,7 +63,7 @@ public function logToVampire_missingData_throwException(): void
*/
public function logToVampire_missingDataInStackTrace_truncateStackTrace(): void
{
$returnValue = AnalyzerLogFormat::logToVampire('{"v":10000,"a":[],"f":"file","l":123,"s":[{"f":"file1","l":1},{"f":"file2"},{"f":"file3","l":3}],"id":"2015-01-01"}', new RootPath(__DIR__));
$returnValue = AnalyzerLogFormat::logToVampire('{"v":10000,"fn":"tombstone","a":[],"f":"file","l":123,"s":[{"f":"file1","l":1},{"f":"file2"},{"f":"file3","l":3}],"id":"2015-01-01"}', new RootPath(__DIR__));

$this->assertCount(1, $returnValue->getStackTrace());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Model/TombstoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private function createTombstone(string $file, string ...$arguments): Tombstone
{
$rootPath = new RootPath(self::ROOT_DIR);

return new Tombstone($arguments, $rootPath->createFilePath($file), 123, 'method');
return new Tombstone('tombstone', $arguments, $rootPath->createFilePath($file), 123, 'method');
}

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getHash_valuesSet_returnCorrectHash(): void
{
$tombstone = $this->createTombstone('file');
$hash = $tombstone->getHash();
$this->assertEquals(596497885, $hash);
$this->assertEquals(1385567777, $hash);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Model/VampireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public function getHash_valuesSet_returnCorrectHash(): void
{
$vampire = Fixture::getVampire();
$hash = $vampire->getHash();
$this->assertEquals(1397077150, $hash);
$this->assertEquals(2405246921, $hash);
}
}
4 changes: 2 additions & 2 deletions tests/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Fixture
public static function getVampire(string ...$arguments): Vampire
{
$rootPath = new RootPath(self::ROOT_DIR);
$tombstone = new Tombstone($arguments, $rootPath->createFilePath('file'), 123, 'method');
$tombstone = new Tombstone('tombstone', $arguments, $rootPath->createFilePath('file'), 123, 'method');
$stackTrace = new StackTrace(new StackTraceFrame($rootPath->createFilePath('/path/to/file1.php'), 11, 'ClassName->method'));

return new Vampire('2015-01-01', 'invoker', $stackTrace, $tombstone, ['metaField' => 'metaValue']);
Expand All @@ -28,7 +28,7 @@ public static function getTombstone(string ...$arguments): Tombstone
{
$rootPath = new RootPath(self::ROOT_DIR);

return new Tombstone($arguments, $rootPath->createFilePath('file'), 123, 'method');
return new Tombstone('tombstone', $arguments, $rootPath->createFilePath('file'), 123, 'method');
}

public static function getTraceFixture(): array
Expand Down
14 changes: 7 additions & 7 deletions tests/Logger/Graveyard/BufferedGraveyardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function tombstone_tombstoneInvoked_notAddToInnerGraveyard(): void
->expects($this->never())
->method($this->anything());

$this->graveyard->tombstone(['args'], ['trace1'], ['metaField' => 'metaValue']);
$this->graveyard->tombstone('tombstone', ['args'], ['trace1'], ['metaField' => 'metaValue']);
}

/**
Expand All @@ -48,10 +48,10 @@ public function tombstone_autoFlushEnabled_directlyAddToInnerGraveyard(): void
$this->innerGraveyard
->expects($this->once())
->method('tombstone')
->with(['args'], ['trace1'], ['metaField' => 'metaValue']);
->with('tombstone', ['args'], ['trace1'], ['metaField' => 'metaValue']);

$this->graveyard->setAutoFlush(true);
$this->graveyard->tombstone(['args'], ['trace1'], ['metaField' => 'metaValue']);
$this->graveyard->tombstone('tombstone', ['args'], ['trace1'], ['metaField' => 'metaValue']);
}

/**
Expand All @@ -63,18 +63,18 @@ public function flush_tombstonesBuffered_addBufferedTombstonesAndFlush(): void
->expects($this->exactly(2))
->method('tombstone')
->withConsecutive(
[['args'], ['trace1'], ['metaField' => 'metaValue1']],
[['args'], ['trace2'], ['metaField' => 'metaValue2']]
['tombstone', ['args'], ['trace1'], ['metaField' => 'metaValue1']],
['tombstone', ['args'], ['trace2'], ['metaField' => 'metaValue2']]
);

$this->innerGraveyard
->expects($this->exactly(2))
->method('flush');

$this->graveyard->tombstone(['args'], ['trace1'], ['metaField' => 'metaValue1']);
$this->graveyard->tombstone('tombstone', ['args'], ['trace1'], ['metaField' => 'metaValue1']);
$this->graveyard->flush();

$this->graveyard->tombstone(['args'], ['trace2'], ['metaField' => 'metaValue2']);
$this->graveyard->tombstone('tombstone', ['args'], ['trace2'], ['metaField' => 'metaValue2']);
$this->graveyard->flush();
}
}
8 changes: 4 additions & 4 deletions tests/Logger/Graveyard/GraveyardBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function build_withHandler_logTombstonesToHandler(): void
->expects($this->once())
->method('log');

$graveyard->tombstone([], Fixture::getTraceFixture(), []);
$graveyard->tombstone('tombstone', [], Fixture::getTraceFixture(), []);
}

/**
Expand All @@ -95,7 +95,7 @@ public function build_withLogger_logExceptionsToLogger(): void
->expects($this->once())
->method('error');

$graveyard->tombstone([], Fixture::getTraceFixture(), []);
$graveyard->tombstone('tombstone', [], Fixture::getTraceFixture(), []);
}

/**
Expand All @@ -115,7 +115,7 @@ public function build_stackTraceDepthSet_logTruncatedStackTrace(): void
->stackTraceDepth(2)
->build();

$graveyard->tombstone([], Fixture::getTraceFixture(), []);
$graveyard->tombstone('tombstone', [], Fixture::getTraceFixture(), []);
}

/**
Expand All @@ -134,7 +134,7 @@ public function build_rootDirSet_logRelativePaths(): void
->withHandler($handler)
->build();

$graveyard->tombstone([], Fixture::getTraceFixture(), []);
$graveyard->tombstone('tombstone', [], Fixture::getTraceFixture(), []);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Logger/Graveyard/GraveyardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public function tombstone_traceGiven_createVampire(): void
$this->vampireFactory
->expects($this->once())
->method('createFromCall')
->with(['label'], $trace, ['metaField' => 'metaValue'])
->with('tombstone', ['label'], $trace, ['metaField' => 'metaValue'])
->willReturn($this->createMock(Vampire::class));

$this->graveyard->tombstone(['label'], $trace, ['metaField' => 'metaValue']);
$this->graveyard->tombstone('tombstone', ['label'], $trace, ['metaField' => 'metaValue']);
}

/**
Expand All @@ -91,7 +91,7 @@ public function tombstone_handlersRegistered_callAllHandlers(): void
->with($this->identicalTo($vampire));

$trace = Fixture::getTraceFixture();
$this->graveyard->tombstone(['label'], $trace, ['metaField' => 'metaValue']);
$this->graveyard->tombstone('tombstone', ['label'], $trace, ['metaField' => 'metaValue']);
}

/**
Expand All @@ -112,7 +112,7 @@ public function tombstone_exceptionHappened_logError(): void
->with('Exception while tracking a tombstone call: Exception message (123)');

$trace = Fixture::getTraceFixture();
$this->graveyard->tombstone([], $trace, []);
$this->graveyard->tombstone('tombstone', [], $trace, []);
}

/**
Expand Down
Loading

0 comments on commit 299b924

Please sign in to comment.