Meta::getAppDir()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\AppMeta;
6
7
use BEAR\AppMeta\Exception\AppNameException;
8
use BEAR\AppMeta\Exception\NotWritableException;
9
use ReflectionClass;
10
11
use function assert;
12
use function class_exists;
13
use function dirname;
14
use function file_exists;
15
use function is_dir;
16
use function mkdir;
17
use function sprintf;
18
19
use const DIRECTORY_SEPARATOR;
20
21
/**
22
 * @psalm-import-type AppName from Types
23
 * @psalm-import-type Context from Types
24
 * @psalm-import-type AppDir from Types
25
 */
26
final class Meta extends AbstractAppMeta
27
{
28
    /**
29
     * @param AppName $name    application name      (Vendor\Project)
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\AppName 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...
30
     * @param Context $context application context   (prod-hal-app)
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\Context 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...
31
     * @param string  $appDir  application directory
32
     */
33
    public function __construct(string $name, string $context = 'app', string $appDir = '')
34
    {
35
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name of type string is incompatible with the declared type BEAR\AppMeta\AppName of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->appDir = $appDir ?: $this->getAppDir($name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $appDir ?: $this->getAppDir($name) of type string is incompatible with the declared type BEAR\AppMeta\AppDir of property $appDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->tmpDir = $this->appDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->appDir . DIRECTOR...RY_SEPARATOR . $context of type string is incompatible with the declared type BEAR\AppMeta\TmpDir of property $tmpDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, 0777, true) && ! is_dir($this->tmpDir)) {
39
            throw new NotWritableException($this->tmpDir);
40
        }
41
42
        $this->logDir = $this->appDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . $context;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->appDir . DIRECTOR...RY_SEPARATOR . $context of type string is incompatible with the declared type BEAR\AppMeta\LogDir of property $logDir.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
        if (! file_exists($this->logDir) && ! @mkdir($this->logDir, 0777, true) && ! is_dir($this->logDir)) {
44
            throw new NotWritableException($this->logDir); // @codeCoverageIgnore
45
        }
46
    }
47
48
    /**
49
     * @param AppName $name
50
     *
51
     * @return AppDir
0 ignored issues
show
Bug introduced by
The type BEAR\AppMeta\AppDir 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...
52
     */
53
    private function getAppDir(string $name): string
54
    {
55
        $module = $name . '\Module\AppModule';
56
        if (! class_exists($module)) {
57
            throw new AppNameException($name);
58
        }
59
60
        $fileName = (new ReflectionClass($module))->getFileName();
61
        assert($fileName !== false, sprintf('Cannot locate AppModule file: %s', $module));
62
63
        /** @var AppDir $dir */
64
        $dir = dirname($fileName, 3);
65
        assert($dir !== '.');
66
67
        return $dir;
68
    }
69
}
70