Inventory_model   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 27.37 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 26
loc 95
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get_category_list() 0 6 1
A get_category_name() 0 14 2
A get_product_list() 0 7 1
A get_product_count() 0 6 1
A get_product_item() 0 6 1
A is_available_product_item() 0 13 2
A get_product_by_search() 14 14 2
A get_count_by_search() 12 12 2

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
/**
4
 * @property CI_DB $db
5
 */
6
class Inventory_model extends CI_Model {
7
8
	public function __construct()
9
	{
10
		parent::__construct();
11
		$this->load->database();
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<Inventory_model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
12
	}
13
14
	public function get_category_list()
15
	{
16
		$this->db->order_by('id');
17
		$query = $this->db->get('category');
18
		return $query->result();
19
	}
20
21
	public function get_category_name($id)
22
	{
23
		$this->db->select('name');
24
		$this->db->where('id', $id);
25
		$query = $this->db->get('category');
26
		$row = $query->row();
27
28
		if ($row === null)
29
		{
30
			show_error('不正な入力です。');
31
		}
32
33
		return $row->name;
34
	}
35
36
	public function get_product_list($cat_id, $limit, $offset)
37
	{
38
		$this->db->where('category_id', $cat_id);
39
		$this->db->order_by('id');
40
		$query = $this->db->get('product', $limit, $offset);
41
		return $query->result();
42
	}
43
44
	public function get_product_count($cat_id)
45
	{
46
		$this->db->where('category_id', $cat_id);
47
		$query = $this->db->get('product');
48
		return $query->num_rows();
49
	}
50
51
	public function get_product_item($id)
52
	{
53
		$this->db->where('id', $id);
54
		$query = $this->db->get('product');
55
		return $query->row();
56
	}
57
58
	public function is_available_product_item($id)
59
	{
60
		$this->db->where('id', $id);
61
		$query = $this->db->get('product');
62
		if ($query->num_rows() == 1)
63
		{
64
			return TRUE;
65
		}
66
		else
67
		{
68
			return FALSE;
69
		}
70
	}
71
72 View Code Duplication
	public function get_product_by_search($q, $limit, $offset)
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...
73
	{
74
# 検索キーワードをスペースで分割し、like()メソッドでLIKE句を指定します。
75
# 複数回like()メソッドを呼んだ場合は、AND条件になります。
76
# name LIKE '%{$keyword}%' AND name LIKE '%{$keyword}%' というSQL文になります。
77
		$keywords = explode(" ", $q);
78
		foreach ($keywords as $keyword)
79
		{
80
			$this->db->like('name', $keyword);
81
		}
82
		$this->db->order_by('id');
83
		$query = $this->db->get('product', $limit, $offset);
84
		return $query->result();
85
	}
86
87 View Code Duplication
	public function get_count_by_search($q)
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...
88
	{
89
		$this->db->select('name');
90
		$keywords = explode(" ", $q);
91
		foreach ($keywords as $keyword)
92
		{
93
			$this->db->like('name', $keyword);
94
		}
95
		$this->db->order_by('id');
96
		$query = $this->db->get('product');
97
		return $query->num_rows();
98
	}
99
100
}
101