SchemaTestClassWriter::write()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 62
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 62
rs 8.6652
cc 5
eloc 26
nc 3
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 SchemaTestClassWriter.
15
 */
16
class SchemaTestClassWriter extends SchemaWriter
17
{
18
    /**
19
     * @param SchemaRdfaData $schemaData
20
     */
21
    public function write(SchemaRdfaData $schemaData)
22
    {
23
        foreach ($schemaData->getClassesWithPropertyNames() as $class) {
24
            $className = $class->name;
25
            $methods = [];
26
27
            if (!empty($class->properties)) {
28
                foreach ($class->properties as $property) {
29
                    $propertyName = $property['name'];
30
                    $propertyNameMethodName = $propertyName;
31
                    $propertyNameMethodName[0] = strtoupper($propertyNameMethodName[0]);
32
33
                    if (empty($usableProperties[$propertyNameMethodName])) {
34
                        $methods[$propertyName] = <<<PHP
35
    public function test{$propertyNameMethodName}WillReturnMappingObject()
36
    {
37
        \$this->assertInstanceOf(Mapping::class, {$className}::{$propertyName}());
38
    }
39
PHP;
40
                    }
41
                }
42
            }
43
44
            ksort($methods, SORT_REGULAR);
45
            $methods = implode("\n\n", $methods);
46
47
            $schemaData = (array) $class;
48
49
            $phpCode = <<<PHP
50
<?php
51
/**
52
 * Author: Nil Portugués Calderó <[email protected]>
53
 * Date: 12/18/15
54
 * Time: 11:36 PM.
55
 *
56
 * For the full copyright and license information, please view the LICENSE
57
 * file that was distributed with this source code.
58
 */
59
60
namespace NilPortugues\Tests\SchemaOrg\Classes;
61
62
use NilPortugues\\SchemaOrg\\Classes\\{$schemaData['name']};
63
use NilPortugues\\SchemaOrg\\Mapping;
64
65
/**
66
 * Classes {$schemaData['name']}Test
67
 * @package NilPortugues\\Tests\\SchemaOrg\\Classes
68
 */
69
class {$schemaData['name']}Test extends \PHPUnit_Framework_TestCase
70
{
71
    public function testSchemaUrlReturnsExpectedUrl()
72
    {
73
        \$this->assertEquals({$schemaData['name']}::schemaUrl(), "{$schemaData['url']}");
74
    }
75
76
$methods
77
}
78
PHP;
79
80
            $this->fileSystem->write($this->savePath.DIRECTORY_SEPARATOR.$className.'Test.php', $phpCode);
81
        }
82
    }
83
}
84