SchemaPropertyWriter::write()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 55
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 55
rs 9.0781
cc 4
eloc 21
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/18/15
5
 * Time: 10:46 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\SchemaOrg\Generator;
12
13
/**
14
 * Class SchemaPropertyWriter.
15
 */
16
class SchemaPropertyWriter extends SchemaWriter
17
{
18
    /**
19
     * @param SchemaRdfaData $schemaData
20
     */
21
    public function write(SchemaRdfaData $schemaData)
22
    {
23
        foreach ($schemaData->getProperties() as $property) {
24
            $className = $property['name'];
25
            $className[0] = strtoupper($className[0]);
26
27
            $allowed = [];
28
            foreach ($property['usedOnClass'] as $belongs) {
29
                if ('Type' === substr($belongs, -4)) {
30
                    $belongs = substr($belongs, 0, -4);
31
                }
32
33
                $allowed[] = "\t\t'".'http://schema.org/'.$belongs."'";
34
            }
35
            $allowed = implode(",\n", $allowed);
36
37
            $phpPropertyCode = <<<PHP
38
<?php
39
/**
40
 * Author: Nil Portugués Calderó <[email protected]>
41
 * Date: 12/18/15
42
 * Time: 11:36 PM.
43
 *
44
 * For the full copyright and license information, please view the LICENSE
45
 * file that was distributed with this source code.
46
 */
47
48
namespace NilPortugues\SchemaOrg\Properties;
49
50
use NilPortugues\SchemaOrg\SchemaProperty;
51
52
/**
53
 * {$property['doc']}
54
 */
55
class {$className}Property extends SchemaProperty
56
{
57
    const SCHEMA_URL = "{$property['url']}";
58
    const PROPERTY_NAME = "{$property['name']}";
59
60
    /**
61
     * A list of schemas allowed to use this property.
62
     *
63
     * @var array
64
     */
65
    protected static \$allowedSchemas = [
66
{$allowed}
67
    ];
68
}
69
PHP;
70
            $this->fileSystem->write(
71
                $this->savePath.DIRECTORY_SEPARATOR.$className.'Property.php',
72
                $phpPropertyCode
73
            );
74
        }
75
    }
76
}
77