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.
Passed
Pull Request — master (#2899)
by
unknown
03:18
created

defineFromEnv()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
/*
4
 This pre-boot script looks for the symphony_preboot_config path environment
5
 variable which is loaded and parsed. Currently it only supports
6
 including additional files but in future it might include other tasks.
7
8
 To use the pre-boot behaviour, follow these steps:
9
10
 1. Set `symphony_enable_preboot` to 1 either via apache envvars or .htaccess
11
 2. Set the path to the pre-boot JSON file with the `symphony_preboot_config`
12
    environment variable.
13
 3. Create the file `symphony_preboot_config` and specify files to include
14
15
 E.g. In the Symphony .htaccess file
16
17
 SetEnv symphony_enable_preboot 1
18
 SetEnv symphony_preboot_config "/path/to/the/preboot.json"
19
20
 Here is an example of the pre-boot config:
21
22
 {
23
     "includes": [
24
         "manifest/preboot/01_test.php",
25
         "/var/www/html/symphony/manifest/preboot/02_test.php"
26
     ]
27
 }
28
29
 Note, at this stage the Symphony core has not been initialised. There is
30
 no database connection and the main autoloader has not been included.
31
 */
32
33
declare(strict_types=1);
34
35
use pointybeard\Helpers\Functions\Json;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Json. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type pointybeard\Helpers\Functions\Json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
37
$isPrebootEnabled = false != getenv('symphony_enable_preboot')
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing getenv('symphony_enable_preboot') of type string to the boolean false. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
38
    ? (bool) intval(getenv('symphony_enable_preboot'))
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
39
    : false
40
;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "false;" but found "false
;"
Loading history...
41
42
// If pre-booting is not enabled, then just return.
43
if (false == $isPrebootEnabled) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
44
    return;
45
}
46
47
/*
48
 * Helper method for creating defines from environment variables
49
 * @param  string $name             The name of the environment variables
50
 * @param  string $default          Optional default value if hasn't been set
51
 * @param  string $customDefineName Optonally set the name of the define.
52
 *                                  Default is to use $name
53
 * @throws  Exception               If $name is not a valid environment variable
54
 *                                  and there is no $default value set, an
55
 *                                  exception is thrown
56
 */
57
if (!function_exists('defineFromEnv')) {
58
    function defineFromEnv(string $name, string $default = null, string $customDefineName = null): void
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$default" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$default"; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between argument "$customDefineName" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$customDefineName"; expected 0 but found 1
Loading history...
59
    {
60
        $value = false !== getenv($name)
61
            ? getenv($name)
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
62
            : $default
63
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected "$default;" but found "$default
;"
Loading history...
64
65
        if (null == $value) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $value of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
66
            throw new Exception("Environment variable {$name} has not been set and there is no default value. Set {$name} with either `export {$name}=xxx` or a line in Apache envvars.");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
67
        }
68
69
        define(
70
            (null != $customDefineName ? $customDefineName : $name),
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
Bug introduced by
It seems like you are loosely comparing $customDefineName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
71
            $value
72
        );
73
    }
74
}
75
76
try {
77
    defineFromEnv('symphony_preboot_config', null, 'SYMPHONY_PREBOOT_CONFIG');
78
} catch (Exception $ex) {
79
    // There was no environment variable set, so we dont need to do anything
80
    // else.
81
    return;
82
}
83
84
try {
85
    // Load the pre-boot config file. Expected to be valid JSON.
86
    $config = Json\json_decode_file(SYMPHONY_PREBOOT_CONFIG);
0 ignored issues
show
Bug introduced by
The function json_decode_file was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
    $config = /** @scrutinizer ignore-call */ Json\json_decode_file(SYMPHONY_PREBOOT_CONFIG);
Loading history...
Bug introduced by
The constant SYMPHONY_PREBOOT_CONFIG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
87
88
    // Make sure 'includes' item is set to avoid errors further down
89
    $config->includes = $config->includes ?? [];
90
91
    // Check each include to make sure its valid
92
    foreach ($config->includes as $ii => $path) {
93
        $path = realpath($path);
94
95
        if (false == $path) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $path of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
96
            throw new Exception('Pre-boot config contains an invalid include: %s'.$config->includes[$ii]);
97
        }
98
99
        $config->includes[$ii] = $path;
100
    }
101
102
    // Check for duplicates
103
    if (count($config->includes) > count(array_unique($config->includes))) {
104
        throw new Exception('Duplicate include detected in pre-boot config '.SYMPHONY_PREBOOT_CONFIG);
105
    }
106
107
    // All checks have passed. Include each file.
108
    foreach ($config->includes as $path) {
109
        include $path;
110
    }
111
} catch (Exception $ex) {
112
    // Failed to load the pre-boot config. We need to handle this here
113
    echo $ex->getMessage().PHP_EOL;
114
    exit;
115
}
116
117
// All done. Resume rest of the Symphony boot process
118