SlidePage::getPortletImage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
* Defines the StaffPage page type.
4
*/
5
class SlidePage extends Page
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    public static $db = array(
8
        'Caption' => 'Text',
9
    );
10
11
    public static $has_one = array(
12
        'Photo' => 'Image',
13
        'InternalPage' => 'SiteTree',
14
    );
15
16
    public static $allowed_children = 'none';
17
18
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
       'ShowInMenus' => false,
20
       'ShowInSearch' => false,
21
    );
22
23
    public function getThumbnail2()
24
    {
25
        return $this->InternalPage()->getPortletImage()->CMSThumbnail()->Tag;
26
    }
27
28
    public function getCMSFields()
29
    {
30
        Requirements::javascript('slider/javascript/slideredit.js');
31
32
        $fields = parent::getCMSFields();
33
        $existing_photo = null;
34
        $photo_field = null;
0 ignored issues
show
Unused Code introduced by
$photo_field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
        $internal_page = $this->InternalPage();
36
37
        if ($internal_page instanceof RenderableAsPortlet) {
0 ignored issues
show
Bug introduced by
The class RenderableAsPortlet does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38
            error_log('Class implements renderable as a portlet');
39
            $existing_photo = $this->InternalPage()->getPortletImage();
40
        } else {
41
            // check parents recursively
42
            $parents = class_parents($internal_page);
43
            error_log(print_r($parents, 1));
44
        }
45
46
        $fields->addFieldToTab('Root.Main',
47
            new TreeDropdownField('InternalPageID', _t('SlidePage.CHOOSE_INTERNAL_LINK', 'Select a page on the website to link to'), 'SiteTree'));
48
49
        $composite_photoField = null;
0 ignored issues
show
Unused Code introduced by
$composite_photoField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
51
        if (!$existing_photo) {
52
            $photo_field = new UploadField('Photo', _t('SlidePage.PHOTO', 'Photo'));
53
            $photo_info = new LiteralField('PhotoInfo', _t('Slide.PHOTO_INFO', 'If the page you choose to link to has an image already it will appear here'), 'Photo');
0 ignored issues
show
Unused Code introduced by
The call to LiteralField::__construct() has too many arguments starting with 'Photo'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
            $composite_photoField = CompositeField::create($photo_field, $photo_info);
55
        } else {
56
            // FIXME, find a cleaner way of doing this
57
            $composite_photoField = new LiteralField('Thumbnail2', '<div id="Thumbnail2" class="field readonly">
58
    <label class="left" for="Form_EditForm_Thumbnail2">Photo</label>
59
    <div class="middleColumn">
60
    <span id="Form_EditForm_Thumbnail2" class="readonly">
61
        <img src="'.$existing_photo->SetWidth(400)->URL.'" alt="'.$existing_photo->Title.'" />
62
    </span>
63
    </div>
64
</div>');
65
        }
66
67
        $composite_photoField->setTitle('Photo');
68
        $fields->addFieldToTab('Root.Main', $composite_photoField);
69
        $fields->addFieldToTab('Root.Main', new TextField('Caption', _t('SlidePage.CAPTION', 'Caption')));
70
        $fields->renameField('Title', 'Slide Title');
71
72
        $fields->removeFieldFromTab('Root.Main', 'Content');
73
74
        return $fields;
75
    }
76
77
    /*
78
    Accessible from templates as $PortletImage
79
    */
80
    public function getPortletImage()
81
    {
82
        $image = null;
0 ignored issues
show
Unused Code introduced by
$image is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
        if ($this->InternalPage() instanceof RenderableAsPortlet) {
0 ignored issues
show
Bug introduced by
The class RenderableAsPortlet does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
84
            $image = $this->InternalPage()->getPortletImage();
85
        } else {
86
            $image = $this->Photo();
87
        }
88
89
        return $image;
90
    }
91
92
    public function getThumbnail()
93
    {
94
        if ($Image = $this->Photo()) {
95
            return $Image->CMSThumbnail();
96
        } else {
97
            return '(No Image)';
98
        }
99
    }
100
101
    public function getWebsiteAddress()
102
    {
103
        $result = '#"';
104
105
        if ($this->InternalPageID) {
106
            $targetPage = DataObject::get_by_id('Page', $this->InternalPageID);
107
            if ($targetPage) {
108
                $result = $targetPage->Link();
109
            } else {
110
                $result = '#';
111
            }
112
        }
113
114
        return $result;
115
    }
116
}
117
118
class SlidePage_Controller extends Page_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
119
{
120
    private static $allowed_actions = array('newpageselected' => true);
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
121
122
    /*
123
    When a new item is selected return JSON containing the title and image
124
    */
125
    public function newpageselected(SS_HTTPRequest $request)
126
    {
127
        $sitetree_id = $request->param('ID');
128
        $page = SiteTree::get_by_id($sitetree_id);
129
        $result = array();
130
        if ($page) {
131
            $result['Title'] = $page->Title;
132
        }
133
        return json_encode($result);
134
    }
135
}
136