Completed
Pull Request — master (#2)
by Beñat
02:38
created

FormsTable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 16.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 14
loc 87
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A no_items() 0 4 1
A column_default() 0 10 3
A get_columns() 0 7 1
A get_sortable_columns() 0 7 1
A prepare_items() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace LIN3S\WPSymfonyForm\Admin\Views;
4
5
use LIN3S\WPSymfonyForm\Admin\Storage\Storage;
6
7
if (!class_exists('WP_List_Table')) {
8
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
9
}
10
11
/**
12
 * Forms table.
13
 *
14
 * @author Beñat Espiña <[email protected]>
15
 */
16
class FormsTable extends \WP_List_Table
17
{
18
    /**
19
     * The storage.
20
     *
21
     * @var Storage
22
     */
23
    private $storage;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param Storage $storage The storage
29
     */
30
    public function __construct(Storage $storage)
31
    {
32
        parent::__construct([
33
            'singular' => __('Form', \WPSymfonyForm::TRANSLATION_DOMAIN),
34
            'plural'   => __('Forms', \WPSymfonyForm::TRANSLATION_DOMAIN),
35
            'ajax'     => false,
36
        ]);
37
38
        $this->storage = $storage;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function no_items()
45
    {
46
        _e('No forms register yet', \WPSymfonyForm::TRANSLATION_DOMAIN);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function column_default($item, $column_name)
53
    {
54
        switch ($column_name) {
55
            case 'name':
56
            case 'link':
57
                return $item[$column_name];
58
            default:
59
                return print_r($item, true);
60
        }
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function get_columns()
67
    {
68
        return [
69
            'name' => __('Name', \WPSymfonyForm::TRANSLATION_DOMAIN),
70
            'link' => __('Link', \WPSymfonyForm::TRANSLATION_DOMAIN),
71
        ];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function get_sortable_columns()
78
    {
79
        return [
80
            'name' => ['name', true],
81
            'link' => ['link', false],
82
        ];
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 View Code Duplication
    public function prepare_items()
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...
89
    {
90
        $this->_column_headers = $this->get_column_info();
91
92
        $limit = $this->get_items_per_page('forms_per_page', 10);
93
        $offset = $this->get_pagenum();
94
        $this->items = $this->storage->findAll($limit, $offset);
95
        $total = count($this->items);
96
97
        $this->set_pagination_args([
98
            'total_items' => $total,
99
            'per_page'    => $limit,
100
        ]);
101
    }
102
}
103