SearchableClass   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%
Metric Value
wmc 3
lcom 1
cbo 9
dl 0
loc 56
ccs 0
cts 23
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 41 1
A IsInSiteTreeHumanReadable() 0 3 2
1
<?php
2
3
/**
4
* Represents a searchable class in Elastica for editing purposes
5
*/
6
class SearchableClass extends DataObject {
7
8
	private static $db = array('Name' => 'Varchar', 'InSiteTree' => 'Boolean');
9
10
	private static $has_many = array('SearchableFields' => 'SearchableField');
0 ignored issues
show
Unused Code introduced by
The property $has_many 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...
11
12
	private static $display_fields = array('Name', 'IsInSiteTreeHumanReadable');
0 ignored issues
show
Unused Code introduced by
The property $display_fields 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...
13
14
15
	public function getCMSFields() {
16
17
		$fields = new FieldList();
18
19
		$fields->push( new TabSet( "Root", $mainTab = new Tab( "Main" ) ) );
20
		$mainTab->setTitle( _t( 'SiteTree.TABMAIN', "Main" ) );
21
22
		$fields->addFieldToTab( 'Root.Main',  $nf = new TextField( 'Name', 'Name') );
23
		$nf->setReadOnly(true);
24
		$nf->setDisabled(true);
25
26
		$config = GridFieldConfig_RecordEditor::create();
27
28
		// remove add button
29
		$config->removeComponent($config->getComponentByType('GridFieldAddNewButton'));
30
		$config->removeComponent($config->getComponentByType('GridFieldDeleteAction'));
31
32
		$config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setDisplayFields() does only exist in the following implementations of said interface: GridFieldDataColumns, GridFieldEditableColumns, GridFieldExternalLink.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
33
            'Name' => 'Name',
34
            'Weight' => 'Weighting',
35
            'Searchable' => 'Search this field?'
36
        ));
37
38
39
        $gridField = new GridField(
40
            'SearchableField', // Field name
41
            'Field Name', // Field title
42
            SearchableField::get()->filter('SearchableClassID', $this->ID)->sort('Name'),
43
            $config
44
        );
45
46
        $fields->addFieldToTab('Root.Main', $gridField);
47
48
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
	    $fields = new FieldList();
50
	    $fields->addFieldToTab('Root.Main', new TextField('Name', 'Name'));
51
52
53
        */
54
	    return $fields;
55
	}
56
57
58
	public function IsInSiteTreeHumanReadable() {
59
		return $this->InSiteTree ? 'Yes' : 'No';
0 ignored issues
show
Documentation introduced by
The property InSiteTree does not exist on object<SearchableClass>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
	}
61
}
62