Passed
Push — master ( ce2598...b8296b )
by Francis
01:16
created

RefactorCI   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 20

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 2 1
C run() 0 52 17
A __construct() 0 4 2
1
<?php
2
defined('BASEPATH') OR exit('No direct script access allowed');
3
4
class RefactorCI {
5
6
  /**
7
   * [private description]
8
   * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
9
   */
10
  private $ci;
11
12
  private $primaryKey;
13
14
  function __construct($params=null) {
15
    $this->ci =& get_instance();
0 ignored issues
show
Bug introduced by
The function get_instance was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
    $this->ci =& /** @scrutinizer ignore-call */ get_instance();
Loading history...
16
    $this->ci->load->config("refactor", false, true);
17
    $this->init($params == null ? [] : $params);
18
  }
19
20
  public function init(array $params):void {
21
    $this->primaryKey = $params['primary_key'] ?? 'id';
22
  }
23
  function run(array &$object, string $ruleName):void {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    $rule = $this->ci->config->item("refactor_$ruleName");
25
    if ($rule == null) return; // No need to go further as rule doesn't exist.
26
27
    // Unset
28
    if (isset($rule['unset'])) {
29
      foreach($rule['unset'] as $key) {
30
        unset($object[$key]);
31
      }
32
    }
33
34
    // Replace
35
    if (isset($rule['replace'])) {
36
      foreach ($rule['replace'] as $oldKey => $newKey) {
37
        $object[$newKey] = $object[$oldKey];
38
        unset($object[$oldKey]);
39
      }
40
    }
41
42
    // Bools
43
    if (isset($rule['bools'])) {
44
      foreach($rule['bools'] as $boolKey) {
45
        $object[$boolKey] = $object[$boolKey] == 1 || $object[$boolKey] == 'true';
46
      }
47
    }
48
49
    // Objects
50
    if (isset($rule['objects'])) {
51
      foreach($rule['objects'] as $field => $data) {
52
        $ids = json_decode($object[$field]);
53
        if (is_scalar($ids)) {
54
          // JSON Array wasn't supplied. Let's treat it as a scaler ID.
55
          $this->ci->db->where($this->primaryKey, $ids);
56
          $query = $this->ci->db->get($data['table']);
57
          if ($query->num_rows() == 0) {
58
            $object[$field] = json_encode (json_decode ("{}"));
59
            continue;
60
          }
61
          $object[$field] = $query->result_array()[0];
62
          if (isset($data['refactor'])) $this->run($object[$field], $data['refactor']);
63
          continue;
64
        }
65
        $object[$field] = [];
66
        foreach($ids as $id) {
67
          $this->ci->db->where($this->primaryKey, $id);
68
          $query = $this->ci->db->get($data['table']);
69
          if ($query->num_rows() == 0) {
70
            continue;
71
          }
72
          $object[$field][] = $query->result_array()[0];
73
          // Recursion
74
          if (isset($data['refactor'])) $this->run($object[$field][count($object[$field]) - 1], $data['refactor']);
75
        }
76
      }
77
    }
78
  }
79
}
80
?>
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...
81