Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

SeoHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace mod_seo\classes;
4
5
use MY_Controller;
6
use Seoexpert_model;
7
use Seoexpert_model_products;
8
9
if (!defined('BASEPATH')) {
10
    exit('No direct script access allowed');
11
}
12
13
/**
14
 * Class Helper for mod_seo module
15
 * @property Seoexpert_model_products seoexpert_model_products
16
 * @property Seoexpert_model seoexpert_model
17
 * @uses \MY_Controller
18
 * @author DevImageCms
19
 * @copyright (c) 2014, ImageCMS
20
 * @package ImageCMSModule
21
 */
22
class SeoHelper extends MY_Controller
23
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
24
25
    protected static $_instance;
26
27
    /**
28
     * __construct base object loaded
29
     * @access public
30
     * @author DevImageCms
31
     * @copyright (c) 2013, ImageCMS
32
     */
33
    public function __construct() {
34
        parent::__construct();
35
        /** Load model * */
36
        $this->load->model('seoexpert_model');
37
        $lang = new \MY_Lang();
38
        $lang->load('mod_seo');
39
    }
40
41
    /**
42
     * @return SeoHelper
43
     */
44
    public static function create() {
45
        (null !== self::$_instance) OR self::$_instance = new self();
46
        return self::$_instance;
47
    }
48
49
    /**
50
     * Prepare parent category name (in future all categories in full path)
51
     * @param bool|int $categoryId
52
     * @param bool|string $locale
53
     * @return array|bool - parentCategory
54
     */
55
    public function prepareCategoriesForProductCategory($categoryId = false, $locale = false) {
56
        $res = $this->seoexpert_model_products->getCategoryByIdAndLocale($categoryId, $locale);
57
58
        if ($res) {
59
            $res['parentCategory'] = $res['name'];
60
            unset($res['name']);
61
            return $res;
62
        }
63
        return FALSE;
64
    }
65
66
    /**
67
     * Autocomplete categories
68
     * @return jsone
0 ignored issues
show
Documentation introduced by
Should the return type not be jsone|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...
69
     */
70
    public function autoCompleteCategories() {
71
        $sCoef = $this->input->get('term');
72
        $sLimit = $this->input->get('limit');
73
74
        $categories = $this->seoexpert_model->getCategoriesByIdName($sCoef, $sLimit);
75
76
        if ($categories != false) {
77
            foreach ($categories as $category) {
0 ignored issues
show
Bug introduced by
The expression $categories of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78
                $response[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
                    'value' => html_entity_decode($category['name']),
80
                    'id' => $category['id'],
81
                ];
82
            }
83
            echo json_encode($response);
84
            return;
0 ignored issues
show
introduced by
Function return type is not void, but function is returning void here
Loading history...
85
        }
86
        echo '';
87
    }
88
89
    /**
90
     * Get base settings
91
     * @param bool|string $locale
92
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
93
     */
94 View Code Duplication
    public function getBaseSettings($locale = FALSE) {
95
        if (!$locale) {
96
            $locale = MY_Controller::getCurrentLocale();
97
        }
98
        $langId = $this->seoexpert_model->getLangIdByLocale($locale);
99
        $res = $this->seoexpert_model->getBaseSettings($langId);
100
101
        if ($res) {
102
            return $res;
103
        }
104
        return FALSE;
105
    }
106
107
    /**
108
     *
109
     * @param bool|string $locale
110
     * @param bool|array $settings
111
     * @return bool
112
     */
113 View Code Duplication
    public function setBaseSettings($locale = FALSE, $settings = FALSE) {
114
        if (!$locale) {
115
            $locale = MY_Controller::getCurrentLocale();
116
        }
117
        $langId = $this->seoexpert_model->getLangIdByLocale($locale);
118
119
        $res = $this->seoexpert_model->setBaseSettings($langId, $settings);
120
121
        if ($res) {
122
            return $res;
123
        }
124
        return FALSE;
125
    }
126
127
}