Explanation::shortcode_attribute_fields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
class Explanation extends Abbreviation implements Shortcodable
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...
4
{
5
6
7
    private static $singular_name = 'Explanation';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name 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...
8
9
    private static $plural_name = 'Explanations';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name 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...
10
11
12
    public function forTemplate()
13
    {
14
        $template = new SSViewer('Explanation');
15
        return $template->process($this);
16
    }
17
18
    /**
19
     * Parse the shortcode and render as a string, probably with a template
20
     * @param array $arguments the list of attributes of the shortcode
21
     * @param string $content the shortcode content
22
     * @param ShortcodeParser $parser the ShortcodeParser instance
23
     * @param string $shortcode the raw shortcode being parsed
24
     * @return String
0 ignored issues
show
Documentation introduced by
Should the return type not be null|HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
     **/
26 View Code Duplication
    public static function parse_shortcode($arguments, $content, $parser, $shortcode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        if (empty($arguments['id'])) {
29
            return;
30
        }
31
32
        if (array_key_exists('id', $arguments) && $arguments['id']) {
33
            $explanation = Explanation::get()->byID($arguments['id']);
34
        }
35
36
        if (!$explanation) {
0 ignored issues
show
Bug introduced by
The variable $explanation does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
37
            return;
38
        }
39
40
        if (array_key_exists('title', $arguments) && $arguments['title']) {
41
            $explanation->Title = $arguments['title'];
42
        }
43
44
        $template = new SSViewer('ExplanationShortcode');
45
        return $template->process($explanation);
46
    }
47
48
    /**
49
     * returns a list of fields for editing the shortcode's attributes
50
     * @return Fieldlist
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldList|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
51
     **/
52
    public static function shortcode_attribute_fields()
53
    {
54
        //@todo use addnew field to add a new abbr. on the fly
55
    }
56
}
57