GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#214)
by joseph
21:10
created

FileCreationTransaction::setPathCreated()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
cc 3
nc 4
nop 1
crap 3.0261
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator;
4
5
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
6
7
/**
8
 * Class FileCreationTransaction
9
 *
10
 * This class will handle keeping track of created files and then if we have a fatal error,
11
 * it will allow us to more easily remove the created files so we are not left with broken generated code
12
 *
13
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator
14
 */
15
class FileCreationTransaction
16
{
17
    /**
18
     * @var array List of paths that have been created
19
     */
20
    private static $pathsCreated = [];
21
22
    /**
23
     * @var bool Have we registered the shutdown function
24
     */
25
    private static $registered = false;
26
27
    /**
28
     * @var float Time the first file was created in float unix time
29
     */
30
    private static $startTime = 0.0;
31
32 4
    public static function getTransaction(): array
33
    {
34 4
        return self::$pathsCreated;
35
    }
36
37
    /**
38
     * @param string $path The absolute path to the created file or folder
39
     *
40
     * @throws DoctrineStaticMetaException if the path does not exist
41
     */
42 8
    public static function setPathCreated(string $path): void
43
    {
44 8
        if (!self::$registered) {
45 2
            self::$registered = self::registerShutdownFunction();
46
        }
47 8
        $realPath = realpath($path);
48 8
        if (!$realPath) {
49
            throw new DoctrineStaticMetaException("path $path does not seem to exist");
50
        }
51 8
        self::$pathsCreated[$realPath] = $realPath;
52 8
    }
53
54
    /**
55
     * Registers our shutdown function. Will attempt to echo out the dirty file clean up commands on a fatal error
56
     *
57
     * @return bool
58
     */
59 2
    private static function registerShutdownFunction(): bool
60
    {
61 2
        self::$startTime = microtime(true);
62 2
        register_shutdown_function(
63
            static function () {
64
                $error = error_get_last();
65
                if ($error === E_ERROR && count(self::$pathsCreated) > 0) {
66
                    self::echoDirtyTransactionCleanupCommands();
67
                }
68 2
            }
69
        );
70
71 2
        return true;
72
    }
73
74
    /**
75
     * Echos out bash find commands to find and delete created paths
76
     *
77
     * @param bool|resource $handle
78
     */
79 4
    public static function echoDirtyTransactionCleanupCommands($handle = STDERR): void
80
    {
81 4
        if (0 === count(self::$pathsCreated)) {
82 2
            return;
83
        }
84 2
        $sinceTimeSeconds = ceil(microtime(true) - self::$startTime);
85
        // why, because of xdebug break - you could easily spend over 1 minute stepping through
86 2
        $sinceTimeMinutes = ceil($sinceTimeSeconds / 60);
87 2
        $pathsToSearch    = [];
88 2
        foreach (self::$pathsCreated as $path) {
89 2
            $realPath = realpath($path);
90 2
            if ($realPath) {
91 2
                $pathsToSearch[$realPath] = $realPath;
92
            }
93
        }
94 2
        if (0 === count($pathsToSearch)) {
95
            return;
96
        }
97 2
        $findCommand   = 'find ' . implode(' ', $pathsToSearch) . "  -mmin -$sinceTimeMinutes";
98 2
        $line          = str_repeat('-', 15);
99 2
        $deleteCommand = "$findCommand -exec rm -rf";
100 2
        fwrite(
101 2
            $handle,
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type boolean; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

101
            /** @scrutinizer ignore-type */ $handle,
Loading history...
102 2
            "\n$line\n"
103 2
            . "\n\nUnclean File Creation Transaction:"
104 2
            . "\n\nTo find created files:\n$findCommand"
105 2
            . "\n\nTo remove created files:\n$deleteCommand"
106 2
            . "\n\n$line\n\n"
107
        );
108 2
    }
109
110
    /**
111
     * If the transaction is successful, we can clear out our log of created files
112
     */
113 8
    public static function markTransactionSuccessful(): void
114
    {
115 8
        self::$pathsCreated = [];
116 8
    }
117
}
118