PageFields::connector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPFoundation\PostTypes\Fields\Templates;
13
14
use LIN3S\WPFoundation\PostTypes\Fields\Fields;
15
16
/**
17
 * Abstract page fields class that extends the fields basic
18
 * behaviour, implementing the "connector" and "removeScreenAttributes"
19
 * methods with the common use case.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
abstract class PageFields extends Fields
24
{
25
    /**
26
     * Constructor.
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
32
        $newPostId = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT);
33
        $updatePostId = filter_input(INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT);
34
35
        if (isset($newPostId)) {
36
            $postId = absint($newPostId);
37
        } elseif (isset($updatePostId)) {
38
            $postId = absint($updatePostId);
39
        } else {
40
            add_action('add_meta_boxes', [$this, 'addMetaBox']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
41
        }
42
        if (false === isset($postId) || $this->name === get_post_meta($postId, '_wp_page_template', true)) {
0 ignored issues
show
Bug introduced by
The variable $postId 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...
43
            add_action('admin_init', [$this, 'addScreenAttributes']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
44
            add_action('admin_init', [$this, 'removeScreenAttributes']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function connector()
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...
52
    {
53
        return [
54
            [
55
                [
56
                    'param'    => 'post_type',
57
                    'operator' => '==',
58
                    'value'    => 'page',
59
                ],
60
                [
61
                    'param'    => 'page_template',
62
                    'operator' => '==',
63
                    'value'    => $this->name,
64
                ],
65
            ],
66
        ];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function removeScreenAttributes()
73
    {
74
        remove_post_type_support('page', 'comments');
0 ignored issues
show
Unused Code introduced by
The call to remove_post_type_support() has too many arguments starting with 'page'.

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...
Unused Code introduced by
The call to the function remove_post_type_support() seems unnecessary as the function has no side-effects.
Loading history...
75
        remove_post_type_support('page', 'custom-fields');
0 ignored issues
show
Unused Code introduced by
The call to remove_post_type_support() has too many arguments starting with 'page'.

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...
Unused Code introduced by
The call to the function remove_post_type_support() seems unnecessary as the function has no side-effects.
Loading history...
76
        remove_post_type_support('page', 'editor');
0 ignored issues
show
Unused Code introduced by
The call to remove_post_type_support() has too many arguments starting with 'page'.

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...
Unused Code introduced by
The call to the function remove_post_type_support() seems unnecessary as the function has no side-effects.
Loading history...
77
    }
78
79
    /**
80
     * Callback that adds the meta box.
81
     */
82
    public function addMetaBox()
83
    {
84
        add_meta_box(
85
            'lin3s-id-default-meta-box',
86
            'LIN3S INFO',
87
            [$this, 'metaBox'],
88
            'page'
89
        );
90
    }
91
92
    /**
93
     * Callback that prints the meta box.
94
     *
95
     * @param mixed  $post The post
96
     * @param string $box  The box
97
     */
98
    public function metaBox($post, $box)
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $box is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        echo <<<'EOF'
101
<h1 style="text-align: center;">
102
    PLEASE ADD <strong>PAGE TITLE</strong> AND SELECT A TEMPLATE IN THE <strong>TEMPLATE SELECTOR</strong>
103
</h1>
104
EOF;
105
    }
106
}
107