PhpNamespaces   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 81
ccs 40
cts 44
cp 0.9091
rs 10
c 5
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setFileNamespace() 0 4 1
A makeShortName() 0 4 1
A getReference() 0 13 4
B toString() 0 26 7
A add() 0 16 4
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
6
use Swaggest\PhpCodeBuilder\JsonSchema\Palette;
7
8
class PhpNamespaces extends PhpTemplate
9
{
10
    private $fileNamespace = '';
11
    private $namespaces = array();
12
    private $shortNames = array();
13
14 5
    public function add($fullyQualifiedName, $as = null)
15
    {
16 5
        if ($fullyQualifiedName[0] !== '\\') {
17
            $fullyQualifiedName = '\\' . $fullyQualifiedName;
18
        }
19 5
        if (null === $as) {
20 5
            $i = '';
21 5
            $short = $this->makeShortName($fullyQualifiedName);
22 5
            while (isset($this->shortNames[$short . $i])) {
23
                $i++;
24
            }
25 5
            $as = $short . $i;
26
        }
27 5
        $this->namespaces[$fullyQualifiedName] = $as;
28 5
        $this->shortNames[$as] = $fullyQualifiedName;
29 5
        return $this;
30
    }
31
32 5
    private function makeShortName($fqn)
33
    {
34 5
        $path = explode('\\', $fqn);
35 5
        return array_pop($path);
36
    }
37
38 5
    public function getReference($fullyQualifiedName)
39
    {
40 5
        if ($fullyQualifiedName[0] !== '\\') {
41 5
            $fullyQualifiedName = '\\' . $fullyQualifiedName;
42
        }
43 5
        if (!isset($this->namespaces[$fullyQualifiedName])) {
44 5
            if ($fullyQualifiedName === Palette::schemaClass()->getFullyQualifiedName()) {
45
                $this->add($fullyQualifiedName, 'JsonBasicSchema');
46
            } else {
47 5
                $this->add($fullyQualifiedName);
48
            }
49
        }
50 5
        return $this->namespaces[$fullyQualifiedName];
51
    }
52
53 5
    protected function toString()
54
    {
55 5
        $result = '';
56 5
        ksort($this->namespaces);
57 5
        foreach ($this->namespaces as $namespace => $as) {
58 5
            $namespace = trim($namespace, '\\');
59 5
            $short = $this->makeShortName($namespace);
60 5
            if ($short === $as) {
61 5
                $as = '';
62
            }
63 5
            $namespacePieces = explode('\\', $namespace);
64 5
            array_pop($namespacePieces);
65 5
            $packageNamespace = implode('\\', $namespacePieces);
66 5
            if ($this->fileNamespace === '\\' . $packageNamespace) {
67
                continue;
68
            }
69 5
            $renderAs = $as ? ' as ' . $as : '';
70 5
            if (!$renderAs && $namespace === $this->fileNamespace . '\\' . $short) {
71 4
                continue;
72
            }
73
            $result .= <<<PHP
74 5
use {$namespace}{$renderAs};
75
76
PHP;
77
        }
78 5
        return $result;
79
    }
80
81
    /**
82
     * @param string $fileNamespace
83
     * @return PhpNamespaces
84
     */
85 5
    public function setFileNamespace($fileNamespace)
86
    {
87 5
        $this->fileNamespace = $fileNamespace;
88 5
        return $this;
89
    }
90
}