LogRef::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Override;
10
use Stringable;
11
use Throwable;
12
13
use function file_put_contents;
14
use function is_link;
15
use function is_writable;
16
use function sprintf;
17
use function symlink;
18
use function unlink;
19
20
final class LogRef implements Stringable
21
{
22
    private string $ref;
23
24
    public function __construct(Throwable $e)
25
    {
26
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
27
        $this->ref = hash('crc32b', $e::class . $e->getMessage() . $e->getFile() . $e->getLine());
28
    }
29
30
    #[Override]
31
    public function __toString(): string
32
    {
33
        return $this->ref;
34
    }
35
36
    public function log(Throwable $e, RouterMatch $request, AbstractAppMeta $appMeta): void
37
    {
38
        $logRefFile = sprintf('%s/logref.%s.log', $appMeta->logDir, $this->ref);
39
        $log = (string) new ExceptionAsString($e, $request);
40
        @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

40
        /** @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...
41
        $linkFile = sprintf('%s/last.logref.log', $appMeta->logDir);
42
        is_link($linkFile) && is_writable($linkFile) && @unlink($linkFile);
43
        @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

43
        /** @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...
44
    }
45
}
46