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 (#224)
by joseph
22:25
created

FileCreationTransaction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 101
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

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

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