XMLSchemaTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 38.6 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 22
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dataValidateSchemaFiles() 0 14 4
A testValidateSchema() 22 36 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection;
4
5
use DirectoryIterator;
6
use DOMDocument;
7
use PHPUnit\Framework\TestCase;
8
9
class XMLSchemaTest extends TestCase
10
{
11
    public static function dataValidateSchemaFiles()
12
    {
13
        $schemaFiles = [];
14
        $di          = new DirectoryIterator(__DIR__ . '/Fixtures/config/xml');
15
        foreach ($di as $element) {
16
            if (! $element->isFile() || substr($element->getFilename(), -4) !== '.xml') {
17
                continue;
18
            }
19
20
            $schemaFiles[] = [$element->getPathname()];
21
        }
22
23
        return $schemaFiles;
24
    }
25
26
    /**
27
     * @dataProvider dataValidateSchemaFiles
28
     */
29
    public function testValidateSchema($file) : void
30
    {
31
        $found = false;
32
        $dom   = new DOMDocument('1.0', 'UTF-8');
33
        $dom->load($file);
34
35
        $xmlns = 'http://symfony.com/schema/dic/doctrine';
36
37
        $dbalElements = $dom->getElementsByTagNameNS($xmlns, 'dbal');
38 View Code Duplication
        if ($dbalElements->length) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            $dbalDom    = new DOMDocument('1.0', 'UTF-8');
40
            $dbalNode   = $dbalDom->importNode($dbalElements->item(0));
41
            $configNode = $dbalDom->createElementNS($xmlns, 'config');
42
            $configNode->appendChild($dbalNode);
43
            $dbalDom->appendChild($configNode);
44
45
            $ret = $dbalDom->schemaValidate(__DIR__ . '/../../Resources/config/schema/doctrine-1.0.xsd');
46
            $this->assertTrue($ret, 'DoctrineBundle Dependency Injection XMLSchema did not validate this XML instance.');
47
            $found = true;
48
        }
49
50
        $ormElements = $dom->getElementsByTagNameNS($xmlns, 'orm');
51 View Code Duplication
        if ($ormElements->length) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
            $ormDom     = new DOMDocument('1.0', 'UTF-8');
53
            $ormNode    = $ormDom->importNode($ormElements->item(0));
54
            $configNode = $ormDom->createElementNS($xmlns, 'config');
55
            $configNode->appendChild($ormNode);
56
            $ormDom->appendChild($configNode);
57
58
            $ret = $ormDom->schemaValidate(__DIR__ . '/../../Resources/config/schema/doctrine-1.0.xsd');
59
            $this->assertTrue($ret, 'DoctrineBundle Dependency Injection XMLSchema did not validate this XML instance.');
60
            $found = true;
61
        }
62
63
        $this->assertTrue($found, 'Neither <doctrine:orm> nor <doctrine:dbal> elements found in given XML. Are namespaces configured correctly?');
64
    }
65
}
66