Test Failed
Push — master ( ffc597...4abc3b )
by Julien
12:58 queued 09:01
created

Prepare   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 55.88%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 19
eloc 33
c 4
b 1
f 0
dl 0
loc 100
ccs 19
cts 34
cp 0.5588
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B define() 0 5 7
A forwarded() 0 4 3
A __construct() 0 8 1
A php() 0 19 4
A debug() 0 12 3
A initialize() 0 2 1
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Bootstrap;
12
13
use Phalcon\Debug;
14
use Phalcon\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zemit\Bootstrap\Config. 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...
15
use Zemit\Di\Injectable;
16
use Zemit\Events\EventsAwareTrait;
17
18
/**
19
 * Class Prepare
20
 * Prepare raw php stuff early in the bootstrap
21
 *
22
 * @author Julien Turbide <[email protected]>
23
 * @copyright Zemit Team <[email protected]>
24
 *
25
 * @since 1.0
26
 * @version 1.0
27
 *
28
 * @package Zemit\Bootstrap
29
 */
30
class Prepare extends Injectable
31
{
32
    use EventsAwareTrait;
33
    
34
    private static bool $initialized = false;
35
    
36
    /**
37
     * Prepare raw php stuff
38
     * - Initialize
39
     * - Random fixes
40
     * - Define constants
41
     * - Force debug
42
     * - Force PHP settings
43
     */
44 4
    public function __construct()
45
    {
46 4
        $this->initialize();
47 4
        $this->forwarded();
48 4
        $this->define();
49
//        $this->debug();
50
//        $this->php();
51 4
        self::$initialized = true;
52
    }
53
    
54
    /**
55
     * Initialisation
56
     */
57
    public function initialize()
58
    {
59
    }
60
    
61
    /**
62
     * Fix for forwarded https
63
     */
64 4
    protected function forwarded()
65
    {
66 4
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
67
            $_SERVER['HTTPS'] = 'on';
68
        }
69
    }
70
    
71
    /**
72
     * Prepare application environment variables
73
     * - APPLICATION_ENV
74
     * - APP_ENV
75
     * - ENV
76
     */
77 4
    protected function define()
78
    {
79 4
        defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
80 4
        defined('APP_ENV') || define('APP_ENV', (getenv('APP_ENV') ? getenv('APP_ENV') : APPLICATION_ENV));
81 4
        defined('ENV') || define('ENV', (getenv('ENV') ? getenv('ENV') : APPLICATION_ENV));
82
    }
83
    
84
    /**
85
     * Prepare debugging
86
     * - Prepare error reporting and display errors natively with PHP
87
     * - Listen with phalcon debugger
88
     */
89 4
    public function debug(Config $config = null)
90
    {
91 4
        $config ??= $this->config;
92
        
93 4
        if ($config->app->debug || $config->debug->enable) {
0 ignored issues
show
Bug Best Practice introduced by
The property debug does not exist on Zemit\Bootstrap\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property app does not exist on Zemit\Bootstrap\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
            // Enabling error reporting and display
95
            error_reporting(E_ALL);
96
            ini_set('display_errors', 1);
97
        } else {
98
            // Disabling error reporting and display
99 4
            error_reporting(-1);
100 4
            ini_set('display_errors', 0);
101
        }
102
    }
103
    
104
    /**
105
     * Prepare some PHP config
106
     * - Should be initialized only once
107
     *
108
     * @param Config|null $config
109
     * @param false $force
110
     */
111 4
    public function php(Config $config = null, $force = false) : void
112
    {
113 4
        if (!$force && self::$initialized) {
0 ignored issues
show
introduced by
The condition $force is always false.
Loading history...
114 4
            return;
115
        }
116
    
117
        $appConfig = $config->app ?? $this->config->app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on Zemit\Bootstrap\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
119
        if ($appConfig) {
120
            setlocale(LC_ALL, 'fr_CA.' . $appConfig->encoding, 'French_Canada.1252');
121
            date_default_timezone_set($appConfig->timezone ?? 'America/Montreal');
122
            mb_internal_encoding($appConfig->encoding ?? 'UTF-8');
123
            mb_http_output($appConfig->encoding ?? 'UTF-8');
124
            ini_set('memory_limit', $appConfig->memoryLimit ?? '256M');
125
            ini_set('post_max_size', $appConfig->postLimit ?? '20M');
126
            ini_set('upload_max_filesize', $appConfig->postLimit ?? '20M');
127
            ini_set('max_execution_time', $appConfig->timeoutLimit ?? '60');
128
            ini_set('html_errors', $appConfig->htmlErrors ?? 0);
129
            set_time_limit($appConfig->timeoutLimit ?? '60');
0 ignored issues
show
Bug introduced by
It seems like $appConfig->timeoutLimit ?? '60' can also be of type string; however, parameter $seconds of set_time_limit() does only seem to accept integer, 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

129
            set_time_limit(/** @scrutinizer ignore-type */ $appConfig->timeoutLimit ?? '60');
Loading history...
130
        }
131
    }
132
}
133