Completed
Push — master ( a88843...acb1bd )
by Rafael
02:11
created

ArrayDebugger::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php 
2
namespace PHP;
3
4
class ArrayDebugger extends \ArrayObject { 
5
6
  const TYPE_GET    = 'GET';
7
  const TYPE_SET    = 'SET';
8
  const TYPE_EXISTS = 'EXISTS';
9
  const TYPE_UNSET  = 'UNSET';
10
11
  private $logger;
12
13
  public function __construct() {
14
15
    $this->logger = function($data_log) {
16
      return dump($data_log);
17
    };
18
19
    return call_user_func_array('parent::__construct', func_get_args()); 
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
20
  }
21
22
  public function sendLog($type, $key, $value = null, array $backtrace ) {
23
24
    $logger           = $this->logger;
25
    $retorno['type']  = $type;  
0 ignored issues
show
Coding Style Comprehensibility introduced by
$retorno was never initialized. Although not strictly required by PHP, it is generally a good practice to add $retorno = 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...
26
    $retorno['key']   = $key;
27
    $retorno['value'] = $value;
28
    $retorno['file']  = $backtrace[0]['file'];
29
    $retorno['line']  = $backtrace[0]['line'];
30
    return $logger($retorno);
31
  }
32
33
  public function setLogger(\Closure $logger) {
34
    $this->logger = $logger;
35
  }
36
37
  public function offsetGet($name) { 
38
39
    $this->sendLog(static::TYPE_GET, $name, null, debug_backtrace());
40
    return call_user_func_array('parent::offsetGet', func_get_args()); 
41
  } 
42
43
  public function offsetSet($name, $value) { 
44
45
    $this->sendLog(static::TYPE_SET, $name, $value, debug_backtrace());
46
    return call_user_func_array('parent::offsetSet', func_get_args()); 
47
  } 
48
49
  public function offsetExists($name) { 
50
51
    $this->sendLog(static::TYPE_EXISTS, $name, null, debug_backtrace());
52
    return call_user_func_array('parent::offsetExists', func_get_args()); 
53
  } 
54
55
  public function offsetUnset($name) { 
56
57
    $this->sendLog(static::TYPE_UNSET, $name, null, debug_backtrace());
58
    return call_user_func_array('parent::offsetUnset', func_get_args()); 
59
  } 
60
} 
61