Completed
Push — develop ( 3bc77a...996ea2 )
by Jaap
06:58
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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor;
17
18
use Cilex\Application as Cilex;
19
use phpDocumentor\Event\Dispatcher;
20
use phpDocumentor\Event\LogEvent;
21
use phpDocumentor\Parser\Event\PreFileEvent;
22
use phpDocumentor\Translator\Translator;
23
use Psr\Container\ContainerInterface;
24
use Psr\Log\LoggerInterface;
25
use Psr\Log\LogLevel;
26
use RuntimeException;
27
28
/**
29
 * Application class for phpDocumentor.
30
 *
31
 * Can be used as bootstrap when the run method is not invoked.
32
 */
33
class Application extends Cilex
34
{
35 1
    public static function VERSION(): string
36
    {
37 1
        return trim(file_get_contents(__DIR__ . '/../../VERSION'));
38
    }
39
40
    public static function templateDirectory(): string
41
    {
42
        $templateDir = __DIR__ . '/../../data/templates';
43
44
        // when installed using composer the templates are in a different folder
45
        $composerTemplatePath = __DIR__ . '/../../../templates';
46
        if (file_exists($composerTemplatePath)) {
47
            $templateDir = $composerTemplatePath;
48
        }
49
50
        return $templateDir;
51
    }
52
53
    /**
54
     * Initializes all components used by phpDocumentor.
55
     */
56
    public function __construct(LoggerInterface $logger, Translator $translator, ContainerInterface $container)
57
    {
58
        parent::__construct($container);
0 ignored issues
show
$container of type object<Psr\Container\ContainerInterface> is not a sub-type of object<Symfony\Component...ion\ContainerInterface>. It seems like you assume a child interface of the interface Psr\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
59
60
        $this->defineIniSettings();
61
        $this->register(new Plugin\ServiceProvider());
62
63
        Dispatcher::getInstance()->addListener(
64
            'parser.file.pre',
65
            function (PreFileEvent $event) use ($logger) {
66
                $logger->log(LogLevel::INFO, 'Parsing ' . $event->getFile());
67
            }
68
        );
69
70
        Dispatcher::getInstance()->addListener('system.log', function (LogEvent $e) use ($logger, $translator) {
71
            $logger->log($e->getPriority(), $translator->translate($e->getMessage()), $e->getContext());
72
        });
73
74
        $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...
75
    }
76
77
    /**
78
     * Adjust php.ini settings.
79
     *
80
     * @throws RuntimeException
81
     */
82
    protected function defineIniSettings(): void
83
    {
84
        $this->setTimezone();
85
        ini_set('memory_limit', '-1');
86
87
        // this code cannot be tested because we cannot control the system settings in unit tests
88
        // @codeCoverageIgnoreStart
89
        if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') && ini_get('opcache.enable_cli')) {
90
            if (ini_get('opcache.save_comments')) {
91
                ini_set('opcache.load_comments', '1');
92
            } else {
93
                ini_set('opcache.enable', '0');
94
            }
95
        }
96
97
        if (extension_loaded('Zend Optimizer+') && ini_get('zend_optimizerplus.save_comments') === 0) {
98
            throw new RuntimeException('Please enable zend_optimizerplus.save_comments in php.ini.');
99
        }
100
101
        // @codeCoverageIgnoreEnd
102
    }
103
104
    /**
105
     * If the timezone is not set anywhere, set it to UTC.
106
     *
107
     * This is done to prevent any warnings being outputted in relation to using
108
     * date/time functions.
109
     *
110
     * @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
111
     *     default timezone.
112
     *
113
     * @codeCoverageIgnore this method is very hard, if not impossible, to unit test and not critical.
114
     */
115
    protected function setTimezone(): void
116
    {
117
        if (false === ini_get('date.timezone')) {
118
            date_default_timezone_set('UTC');
119
        }
120
    }
121
}
122