Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

src/phpDocumentor/Application.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor;
13
14
use Cilex\Application as Cilex;
15
use phpDocumentor\Event\Dispatcher;
16
use phpDocumentor\Event\LogEvent;
17
use phpDocumentor\Parser\Event\PreFileEvent;
18
use phpDocumentor\Translator\Translator;
19
use Psr\Container\ContainerInterface;
20
use Psr\Log\LoggerInterface;
21
use Psr\Log\LogLevel;
22
23
/**
24
 * Application class for phpDocumentor.
25
 *
26
 * Can be used as bootstrap when the run method is not invoked.
27
 */
28
class Application extends Cilex
29
{
30 1
    public static function VERSION()
31
    {
32 1
        return trim(file_get_contents(__DIR__ . '/../../VERSION'));
33
    }
34
35
    /**
36
     * Initializes all components used by phpDocumentor.
37
     */
38
    public function __construct(LoggerInterface $logger, Translator $translator, ContainerInterface $container)
39
    {
40
        parent::__construct($container);
41
42
        $this->defineIniSettings();
43
        $this->register(new Plugin\ServiceProvider());
44
45
        Dispatcher::getInstance()->addListener(
46
            'parser.file.pre',
47
            function (PreFileEvent $event) use ($logger) {
48
                $logger->log(LogLevel::INFO, 'Parsing ' . $event->getFile());
49
            }
50
        );
51
52
        Dispatcher::getInstance()->addListener('system.log', function (LogEvent $e) use ($logger, $translator) {
53
            $logger->log($e->getPriority(), $translator->translate($e->getMessage()), $e->getContext());
54
        });
55
56
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
$container is of type object<Psr\Container\ContainerInterface>, but the property $container was declared to be of type object<Symfony\Component...ion\ContainerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
57
    }
58
59
    /**
60
     * Adjust php.ini settings.
61
     */
62
    protected function defineIniSettings()
63
    {
64
        $this->setTimezone();
65
        ini_set('memory_limit', -1);
66
67
        // this code cannot be tested because we cannot control the system settings in unit tests
68
        // @codeCoverageIgnoreStart
69
        if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') && ini_get('opcache.enable_cli')) {
70
            if (ini_get('opcache.save_comments')) {
71
                ini_set('opcache.load_comments', 1);
72
            } else {
73
                ini_set('opcache.enable', 0);
74
            }
75
        }
76
77
        if (extension_loaded('Zend Optimizer+') && ini_get('zend_optimizerplus.save_comments') === 0) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of ini_get('zend_optimizerplus.save_comments') (string) and 0 (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
78
            throw new \RuntimeException('Please enable zend_optimizerplus.save_comments in php.ini.');
79
        }
80
81
        // @codeCoverageIgnoreEnd
82
    }
83
84
    /**
85
     * If the timezone is not set anywhere, set it to UTC.
86
     *
87
     * This is done to prevent any warnings being outputted in relation to using
88
     * date/time functions. What is checked is php.ini, and if the PHP version
89
     * is prior to 5.4, the TZ environment variable.
90
     *
91
     * @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
92
     *     default timezone.
93
     *
94
     * @codeCoverageIgnore this method is very hard, if not impossible, to unit test and not critical.
95
     */
96
    protected function setTimezone()
97
    {
98
        if (false === ini_get('date.timezone')
99
            || (version_compare(PHP_VERSION, '5.4.0', '<') && false === getenv('TZ'))
100
        ) {
101
            date_default_timezone_set('UTC');
102
        }
103
    }
104
}
105