SchemaLocator::getPerFileSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File: SchemaLocator.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\XmlUrlRewrites\Model\Config;
12
13
use Magento\Framework\Config\SchemaLocatorInterface;
14
use Magento\Framework\Module\Dir;
15
use Magento\Framework\Module\Dir\Reader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, MSlwk\XmlUrlRewrites\Model\Config\Reader.

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...
16
17
/**
18
 * Class SchemaLocator
19
 * @package MSlwk\XmlUrlRewrites\Model\Config
20
 */
21
class SchemaLocator implements SchemaLocatorInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $schema = null;
27
28
    /**
29
     * @var string
30
     */
31
    protected $perFileSchema = null;
32
33
    /**
34
     * @param Reader $moduleReader
35
     */
36
    public function __construct(Reader $moduleReader)
37
    {
38
        $etcDir = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'MSlwk_XmlUrlRewrites');
39
        $this->schema = $etcDir . '/url_rewrites.xsd';
40
        $this->perFileSchema = $etcDir . '/url_rewrites.xsd';
41
    }
42
43
    /**
44
     * Get path to merged config schema
45
     *
46
     * @return string|null
47
     */
48
    public function getSchema()
49
    {
50
        return $this->schema;
51
    }
52
53
    /**
54
     * Get path to pre file validation schema
55
     *
56
     * @return string|null
57
     */
58
    public function getPerFileSchema()
59
    {
60
        return $this->perFileSchema;
61
    }
62
}
63