Completed
Push — master ( 701b1d...7a5b7f )
by Harald
06:30 queued 03:11
created

d_latest_addons   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 24.55 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 27
loc 110
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A d_latest_addons() 0 9 2
C getOutput() 27 51 8
A isEnabled() 0 3 1
A check() 0 3 1
B install() 0 24 1
A remove() 0 3 1
A keys() 0 3 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
2
/*
3
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2014 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  use OSC\OM\Cache;
14
  use OSC\OM\DateTime;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, DateTime.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
  use OSC\OM\HTML;
16
  use OSC\OM\HTTP;
17
  use OSC\OM\OSCOM;
18
  use OSC\OM\Registry;
19
20
  class d_latest_addons {
21
    var $code = 'd_latest_addons';
22
    var $title;
23
    var $description;
24
    var $sort_order;
25
    var $enabled = false;
26
27
    function d_latest_addons() {
28
      $this->title = MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_TITLE;
29
      $this->description = MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_DESCRIPTION;
30
31
      if ( defined('MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_STATUS') ) {
32
        $this->sort_order = MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_SORT_ORDER;
33
        $this->enabled = (MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_STATUS == 'True');
34
      }
35
    }
36
37
    function getOutput() {
38
      $entries = [];
39
40
      $addonsCache = new Cache('oscommerce_website-addons-latest5');
41
42 View Code Duplication
      if ($addonsCache->exists(360)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
        $entries = $addonsCache->get();
44
      } else {
45
        $response = HTTP::getResponse(['url' => 'https://www.oscommerce.com/index.php?RPC&GetLatestAddons']);
46
47
        if (!empty($response)) {
48
          $response = json_decode($response, true);
49
50
          if (is_array($response) && (count($response) === 5)) {
51
            $entries = $response;
52
          }
53
        }
54
55
        $addonsCache->save($entries);
56
      }
57
58
      $output = '<table class="table table-hover">
59
                   <thead>
60
                     <tr class="info">
61
                       <th>' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_TITLE . '</th>
62
                       <th class="text-right">' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_DATE . '</th>
63
                     </tr>
64
                   </thead>
65
                   <tbody>';
66
67 View Code Duplication
      if (is_array($entries) && (count($entries) === 5)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
        foreach ($entries as $item) {
69
          $output .= '    <tr>
70
                            <td><a href="' . HTML::outputProtected($item['link']) . '" target="_blank">' . HTML::outputProtected($item['title']) . '</a></td>
71
                            <td class="text-right" style="white-space: nowrap;">' . HTML::outputProtected(DateTime::toShort($item['date'])) . '</td>
72
                          </tr>';
73
        }
74
      } else {
75
        $output .= '    <tr>
76
                          <td colspan="2">' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_FEED_ERROR . '</td>
77
                        </tr>';
78
      }
79
80
      $output .= '    <tr>
81
                        <td class="text-right" colspan="2"><a href="http://addons.oscommerce.com" target="_blank" title="' . HTML::outputProtected(MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_ICON_SITE) . '"><span class="fa fa-fw fa-home"></span></a></td>
82
                      </tr>
83
                    </tbody>
84
                  </table>';
85
86
      return $output;
87
    }
88
89
    function isEnabled() {
90
      return $this->enabled;
91
    }
92
93
    function check() {
94
      return defined('MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_STATUS');
95
    }
96
97
    function install() {
98
      $OSCOM_Db = Registry::get('Db');
99
100
      $OSCOM_Db->save('configuration', [
101
        'configuration_title' => 'Enable Latest Add-Ons Module',
102
        'configuration_key' => 'MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_STATUS',
103
        'configuration_value' => 'True',
104
        'configuration_description' => 'Do you want to show the latest osCommerce Add-Ons on the dashboard?',
105
        'configuration_group_id' => '6',
106
        'sort_order' => '1',
107
        'set_function' => 'tep_cfg_select_option(array(\'True\', \'False\'), ',
108
        'date_added' => 'now()'
109
      ]);
110
111
      $OSCOM_Db->save('configuration', [
112
        'configuration_title' => 'Sort Order',
113
        'configuration_key' => 'MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_SORT_ORDER',
114
        'configuration_value' => '0',
115
        'configuration_description' => 'Sort order of display. Lowest is displayed first.',
116
        'configuration_group_id' => '6',
117
        'sort_order' => '0',
118
        'date_added' => 'now()'
119
      ]);
120
    }
121
122
    function remove() {
123
      return Registry::get('Db')->exec('delete from :table_configuration where configuration_key in ("' . implode('", "', $this->keys()) . '")');
124
    }
125
126
    function keys() {
127
      return array('MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_STATUS', 'MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_SORT_ORDER');
128
    }
129
  }
130
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
131