| Conditions | 1 |
| Paths | 1 |
| Total Lines | 68 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 50 | public function testGetAttributes() |
||
| 51 | { |
||
| 52 | $field = UploadField::create('MyField'); |
||
| 53 | $field->addExtraClass('myfield'); |
||
| 54 | $field->setIsMultiUpload(false); |
||
| 55 | $field->setFolderName('/'); |
||
| 56 | /** @var Image $image */ |
||
| 57 | $image = $this->objFromFixture(Image::class, 'image1'); |
||
| 58 | $field->setItems(new ArrayList([$image])); |
||
| 59 | $admin = AssetAdmin::create(); |
||
| 60 | Form::create($admin, 'MyForm', FieldList::create($field), FieldList::create()); |
||
| 61 | |||
| 62 | $attributes = $field->getAttributes(); |
||
| 63 | $schema = [ |
||
| 64 | 'name' => 'MyField', |
||
| 65 | 'id' => 'Form_MyForm_MyField', |
||
| 66 | 'type' => 'file', |
||
| 67 | 'component' => 'UploadField', |
||
| 68 | 'holderId' => 'Form_MyForm_MyField_Holder', |
||
| 69 | 'title' => 'My field', |
||
| 70 | 'source' => null, |
||
| 71 | 'extraClass' => 'entwine-uploadfield uploadfield myfield', |
||
| 72 | 'description' => null, |
||
| 73 | 'rightTitle' => null, |
||
| 74 | 'leftTitle' => null, |
||
| 75 | 'readOnly' => false, |
||
| 76 | 'disabled' => false, |
||
| 77 | 'autoFocus' => false, |
||
| 78 | 'customValidationMessage' => '', |
||
| 79 | 'validation' => [], |
||
| 80 | 'attributes' => [], |
||
| 81 | 'data' => [ |
||
| 82 | 'createFileEndpoint' => [ |
||
| 83 | 'url' => 'admin/assets/MyForm/field/MyField/upload', |
||
| 84 | 'method' => 'post', |
||
| 85 | 'payloadFormat' => 'urlencoded', |
||
| 86 | ], |
||
| 87 | 'multi' => false, |
||
| 88 | 'parentid' => 0, |
||
| 89 | 'maxFilesize' => $field->getAllowedMaxFileSize() / 1024 / 1024, |
||
| 90 | 'maxFiles' => null, |
||
| 91 | 'canUpload' => true, |
||
| 92 | 'canAttach' => true, |
||
| 93 | ], |
||
| 94 | 'schemaType' => 'Custom' |
||
| 95 | ]; |
||
| 96 | $state = [ |
||
| 97 | 'name' => 'MyField', |
||
| 98 | 'id' => 'Form_MyForm_MyField', |
||
| 99 | 'value' => [ 'Files' => [$image->ID] ], |
||
| 100 | 'message' => null, |
||
| 101 | 'data' => [ |
||
| 102 | 'files' => [ $admin->getMinimalistObjectFromData($image) ], |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | $this->assertArraySubset( |
||
| 106 | [ |
||
| 107 | 'class' => 'entwine-uploadfield uploadfield myfield', |
||
| 108 | 'type' => 'file', |
||
| 109 | 'multiple' => false, |
||
| 110 | 'id' => 'Form_MyForm_MyField' |
||
| 111 | ], |
||
| 112 | $attributes |
||
| 113 | ); |
||
| 114 | |||
| 115 | // Check schema / state are encoded in this field |
||
| 116 | $this->assertEquals($schema, json_decode($attributes['data-schema'], true)); |
||
| 117 | $this->assertEquals($state, json_decode($attributes['data-state'], true)); |
||
| 118 | } |
||
| 120 |