DocumentHtmlEditorFieldToolbar   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateLinkForm() 0 36 3
1
<?php
2
/**
3
 * Extends the original toolbar with document picking capability - modified lines are commented.
4
 */
5
class DocumentHtmlEditorFieldToolbar extends Extension
6
{
7
    public function updateLinkForm(Form $form)
8
    {
9
        $linkType = null;
10
        $fieldList = null;
11
        $fields = $form->Fields();//->fieldByName('Heading');
12
        foreach ($fields as $field) {
0 ignored issues
show
Bug introduced by
The expression $fields of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
13
            $linkType = ($field->fieldByName('LinkType'));
14
            $fieldList = $field;
15
            if ($linkType) {
16
                break;
17
            }   //break once we have the object
18
        }
19
20
        $source = $linkType->getSource();
21
        $source['document'] = 'Download a document';
22
        $linkType->setSource($source);
23
24
        $addExistingField = new DMSDocumentAddExistingField('AddExisting', 'Add Existing');
25
        $addExistingField->setForm($form);
26
        $addExistingField->setUseFieldClass(false);
27
        $fieldList->insertAfter($addExistingField, 'Description');
28
29
        $fieldList->push(HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()));
30
31
//		Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/behaviour/behaviour.js");
32
//		Requirements::javascript(SAPPHIRE_DIR . "/javascript/tiny_mce_improvements.js");
33
//
34
//		// create additional field, rebase to 'documents' directory
35
//		$documents = new TreeDropdownField('document', 'Document', 'File', 'ID', 'DocumentDropdownTitle', true);
36
//		$documents->setSearchFunction(array($this, 'documentSearchCallback'));
37
//		$baseFolder = Folder::find_or_make(Document::$directory);
38
//		$documents->setTreeBaseID($baseFolder->ID);
39
40
41
        //return $form;
42
    }
43
}
44