Completed
Push — master ( 8e7ddb...d39b5e )
by Marko
02:50
created

AframeDOMProcessor::correctOutputFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 29, 2016 - 8:06:17 AM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         AframeDOMProcessor.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 ^ @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core\DOM;
25
26
trait AframeDOMProcessor
27
{
28
29
    /**
30
     * Add document comment for formatting
31
     *
32
     * @param string $element            
33
     * @param string $comment            
34
     */
35 4
    protected function appendFormatComment(string $element, string $comment)
36
    {
37 4
        if ($this->formatOutput) {
0 ignored issues
show
Bug introduced by
The property formatOutput does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38 4
            $com = $this->docObj->createComment($comment);
0 ignored issues
show
Bug introduced by
The property docObj does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39 4
            $this->{$element}->appendChild($com);
40
        }
41 4
    }
42
43
    /**
44
     * Correct html format for tags which are not supported by DOMDocument
45
     *
46
     * @param string $html            
47
     * @return string
48
     */
49 4
    protected function correctOutputFormat($html)
50
    {
51
        $tags = array(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52 4
            '<!--',
53
            '-->',
54
            '<a-assets>',
55
            '</a-assets>',
56
            '</a-scene>'
57
        );
58
        $values = array(
59 4
            '',
60
            "\t",
61
            "\n\t<a-assets>",
62
            "\n\t</a-assets>",
63
            "\n</a-scene>"
64
        );
65 4
        return str_ireplace($tags, $values, $html);
66
    }
67
68
    /**
69
     * Prepeare head
70
     *
71
     * @return void
72
     */
73 5
    protected function renderHead()
74
    {
75 5
        $title = $this->docObj->createElement('title', $this->scene_title);
0 ignored issues
show
Bug introduced by
The property scene_title does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76 5
        $this->head->appendChild($title);
0 ignored issues
show
Bug introduced by
The property head does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77 5
        $this->appendDefaultMetaTags();
78 5
        $this->appendCDN();
79 5
    }
80
81
    /**
82
     * Append deffault metatags
83
     *
84
     * @return void
85
     */
86 5
    protected function appendDefaultMetaTags()
87
    {
88 5
        $this->appendMetaTag(array(
89 5
            'name' => 'description',
90 5
            'content' => $this->scene_description
0 ignored issues
show
Bug introduced by
The property scene_description does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
91
        ));
92 5
        foreach ($this->getDefaultMetaTags() as $tag)
93 5
            $this->appendMetaTag($tag);
94 5
    }
95
96
    /**
97
     * Get default meta tags
98
     *
99
     * @return array
100
     */
101 5
    protected function getDefaultMetaTags(): array
102
    {
103 5
        $dmt = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104 5
        $dmt[0]['charset'] = 'utf-8';
105
        
106 5
        $dmt[1]['name'] = 'viewport';
107
        
108 5
        $vp = 'width=device-width,initial-scale=1,maximum-scale=1,shrink-to-fit=no,user-scalable=no,minimal-ui';
109
        
110 5
        $dmt[1]['content'] = $vp;
111 5
        $dmt[2]['name'] = 'mobile-web-app-capable';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
112 5
        $dmt[2]['content'] = 'yes';
113 5
        $dmt[3]['name'] = 'theme-color';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
114 5
        $dmt[3]['content'] = 'black';
115
        
116 5
        return $dmt;
117
    }
118
119
    /**
120
     * If requested by user use aframe CDN
121
     *
122
     * @return void
123
     */
124 5
    protected function appendCDN()
125
    {
126 5
        if ($this->use_cdn) {
0 ignored issues
show
Bug introduced by
The property use_cdn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
127 3
            $cdn_script = $this->docObj->createElement('script');
128 3
            $cdn_script->setAttribute('src', $this->aframe_cdn);
0 ignored issues
show
Bug introduced by
The property aframe_cdn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
129 3
            $this->head->appendChild($cdn_script);
130
        }
131 5
    }
132
133
    /**
134
     * Prepare body
135
     *
136
     * @return void
137
     */
138 5
    protected function renderBody()
139
    {
140 5
        $this->body->appendChild($this->scene);
0 ignored issues
show
Bug introduced by
The property body does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property scene does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
141 5
    }
142
143
    /**
144
     * Create meta tags
145
     *
146
     * @param array $attr            
147
     */
148 5
    protected function appendMetaTag(array $attr)
149
    {
150 5
        $metatag = $this->docObj->createElement('meta');
151 5
        foreach ($attr as $key => $val)
152 5
            $metatag->setAttribute($key, $val);
153 5
        $this->head->appendChild($metatag);
154 5
    }
155
156
    /**
157
     * Creates an empty DOMDocumentType object
158
     *
159
     * @param string $doctype            
160
     * @return void
161
     */
162 71
    protected function createDocType(string $doctype)
163
    {
164 71
        $this->doctypeObj = $this->createDocumentType($doctype);
0 ignored issues
show
Bug introduced by
The property doctypeObj does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like createDocumentType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
165 71
    }
166
167
    /**
168
     * Creates a DOMDocument object of the specified type with its document element
169
     *
170
     * @return void
171
     */
172 71
    protected function createAframeDocument()
173
    {
174 71
        $this->docObj = $this->createDocument(null, 'html', $this->doctypeObj);
0 ignored issues
show
Bug introduced by
It seems like createDocument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
175 71
    }
176
177
    /**
178
     * Create dom elements for DOMDocument
179
     *
180
     * @return void
181
     */
182 71
    protected function documentBootstrap()
183
    {
184
        /* Create <head> element */
185 71
        $this->head = $this->docObj->createElement('head');
186
        /* Create <body> element */
187 71
        $this->body = $this->docObj->createElement('body', $this->formatOutput ? "\n" : '');
188
        /* Create <a-scene> element */
189 71
        $this->scene = $this->docObj->createElement('a-scene');
190
        /* Create <a-assets> element */
191 71
        $this->assets = $this->docObj->createElement('a-assets');
0 ignored issues
show
Bug introduced by
The property assets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
192 71
    }
193
}
194