Conditions | 6 |
Paths | 3 |
Total Lines | 83 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
21 | public function write(SchemaRdfaData $schemaData) |
||
22 | { |
||
23 | foreach ($schemaData->getClassesWithPropertyNames() as $class) { |
||
24 | $className = $class->name; |
||
25 | $methods = []; |
||
26 | $usableProperties = []; |
||
27 | $dynamicCall = []; |
||
28 | $useProperties = []; |
||
29 | |||
30 | if (!empty($class->properties)) { |
||
31 | foreach ($class->properties as $propertyClassName => $property) { |
||
32 | $propertyName = $property['name']; |
||
33 | $parentClassName = $property['parent']; |
||
34 | $propertyNameMethodName = $propertyName; |
||
35 | $propertyNameMethodName[0] = strtoupper($propertyNameMethodName[0]); |
||
36 | |||
37 | $dynamicCall[] = ' * @method static \\NilPortugues\\SchemaOrg\\Properties\\'.$propertyNameMethodName.'Property '.$propertyName.'()'; |
||
38 | |||
39 | if (empty($usableProperties[$propertyNameMethodName])) { |
||
40 | $usableProperties[$propertyNameMethodName] = $propertyClassName; |
||
41 | |||
42 | $schemaClass = '\\NilPortugues\\SchemaOrg\\Classes\\'.$className; |
||
43 | if ($parentClassName !== $className) { |
||
44 | $schemaClass = '\\NilPortugues\\SchemaOrg\\Classes\\'.$parentClassName; |
||
45 | } |
||
46 | |||
47 | $methods[$propertyName] = "\t\t'{$propertyName}' => [\n\t\t\t'propertyClass' => '\\NilPortugues\\SchemaOrg\\Properties\\{$propertyNameMethodName}Property',\n\t\t\t'schemaClass' => '{$schemaClass}',\n\t\t],"; |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | $useProperties[] = 'use NilPortugues\SchemaOrg\Mapping;'; |
||
53 | $useProperties[] = 'use NilPortugues\SchemaOrg\SchemaClass;'; |
||
54 | |||
55 | sort($useProperties, SORT_REGULAR); |
||
56 | $useProperties = implode("\n", array_unique($useProperties)); |
||
57 | ksort($methods, SORT_REGULAR); |
||
58 | $methods = implode("\n", $methods); |
||
59 | $dynamicCall = implode("\n", $dynamicCall); |
||
60 | |||
61 | $schemaData = (array) $class; |
||
62 | |||
63 | $phpCode = <<<PHP |
||
64 | <?php |
||
65 | /** |
||
66 | * Author: Nil Portugués Calderó <[email protected]> |
||
67 | * Date: 12/18/15 |
||
68 | * Time: 11:36 PM. |
||
69 | * |
||
70 | * For the full copyright and license information, please view the LICENSE |
||
71 | * file that was distributed with this source code. |
||
72 | */ |
||
73 | |||
74 | namespace NilPortugues\SchemaOrg\Classes; |
||
75 | |||
76 | {$useProperties} |
||
77 | |||
78 | /** |
||
79 | * METHODSTART. |
||
80 | {$dynamicCall} |
||
81 | * METHODEND. |
||
82 | * |
||
83 | * {$schemaData['doc']} |
||
84 | */ |
||
85 | class {$schemaData['name']} extends SchemaClass |
||
86 | { |
||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected static \$schemaUrl = "{$schemaData['url']}"; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected static \$supportedMethods = [ |
||
96 | {$methods} |
||
97 | ]; |
||
98 | } |
||
99 | PHP; |
||
100 | |||
101 | $this->fileSystem->write($this->savePath.DIRECTORY_SEPARATOR.$className.'.php', $phpCode); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 |