LogRef   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
dl 0
loc 23
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 8 3
A __toString() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Sunday\Extension\Router\RouterMatch;
9
use Stringable;
10
use Throwable;
11
12
use function file_put_contents;
13
use function is_link;
14
use function is_writable;
15
use function sprintf;
16
use function symlink;
17
use function unlink;
18
19
final class LogRef implements Stringable
20
{
21
    private string $ref;
22
23
    public function __construct(Throwable $e)
24
    {
25
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
26
        $this->ref = hash('crc32b', $e::class . $e->getMessage() . $e->getFile() . $e->getLine());
27
    }
28
29
    public function __toString(): string
30
    {
31
        return $this->ref;
32
    }
33
34
    public function log(Throwable $e, RouterMatch $request, AbstractAppMeta $appMeta): void
35
    {
36
        $logRefFile = sprintf('%s/logref.%s.log', $appMeta->logDir, $this->ref);
37
        $log = (string) new ExceptionAsString($e, $request);
38
        @file_put_contents($logRefFile, $log);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

38
        /** @scrutinizer ignore-unhandled */ @file_put_contents($logRefFile, $log);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
        $linkFile = sprintf('%s/last.logref.log', $appMeta->logDir);
40
        is_link($linkFile) && is_writable($linkFile) && @unlink($linkFile);
41
        @symlink($logRefFile, $linkFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for symlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

41
        /** @scrutinizer ignore-unhandled */ @symlink($logRefFile, $linkFile);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
42
    }
43
}
44