Completed
Push — work-fleets ( ad253d...d9c01a )
by SuperNova.WS
06:45
created

Accessors   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
c 8
b 0
f 1
dl 0
loc 104
ccs 0
cts 25
cp 0
rs 10
wmc 10
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 3 1
B set() 0 23 5
A execute() 0 16 4
1
<?php
2
/**
3
 * Created by Gorlum 19.08.2016 20:35
4
 */
5
6
namespace Common;
7
8
/**
9
 * Accessors storage
10
 *
11
 * TODO - make magic method access to accessors ????????
12
 *
13
 * @package Common
14
 */
15
class Accessors {
16
17
  /**
18
   * Array of accessors - getters/setters/etc
19
   *
20
   * @var callable[]
21
   */
22
  protected $accessors = array();
23
24
  /**
25
   * @var bool[]
26
   */
27
  protected $shared = array();
28
29
  /**
30
   * Result of shared function execution
31
   *
32
   * @var array
33
   */
34
  protected $executed = array();
35
36
  /**
37
   * @param string $varName
38
   * @param string $accessor
39
   *
40
   * @return bool
41
   */
42
  public function exists($varName, $accessor) {
43
    return isset($this->accessors[$varName . $accessor]);
44
  }
45
46
  /**
47
   * Assign accessor to a named variable
48
   *
49
   * Different accessors have different signatures - you should look carefully before assigning accessor
50
   *
51
   * @param string   $varName
52
   * @param string   $accessor - type of accessor getter/setter/importer/exporter/etc
53
   * @param callable $callable
54
   *
55
   * @throws \Exception
56
   */
57
  public function set($varName, $accessor, $callable, $shared = false) {
58
    if (empty($callable)) {
59
      return;
60
    } elseif (!is_callable($callable)) {
61
      throw new \Exception('Error assigning callable in ' . get_called_class() . '::set()! Callable typed [' . $accessor . '] is not a callable or not accessible in the scope');
62
    }
63
64
    // Converting method array-callable to closure
65
    // Require PHP 5.4 !!!!!!!!!!
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
//    if (is_array($callable) && count($callable) == 2 && is_object($callable[0])) {
67
//      $method = new \ReflectionMethod($callable[0], $callable[1]);
68
//      $callable = $method->getClosure($callable[0]);
69
//    }
70
71
    if($invoker = Invoker::build($callable)) {
72
      $callable = $invoker;
73
    }
74
75
    $this->accessors[$varName . $accessor] = $callable;
76
    if($shared) {
77
      $this->shared[$varName . $accessor] = true;
78
    }
79
  }
80
81
//  /**
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
//   * Gets accessor for later use
83
//   *
84
//   * @param string $varName
85
//   * @param string $accessor
86
//   *
87
//   * @return callable|null
88
//   */
89
//  public function get($varName, $accessor) {
90
//    return $this->exists($varName, $accessor) ? $this->accessors[$varName . $accessor] : null;
91
//  }
92
93
  /**
94
   * @param string $varName
95
   * @param string $accessor
96
   * @param array  $params
97
   *
98
   * @return mixed
99
   * @throws \Exception
100
   */
101
  public function execute($varName, $accessor, $params) {
102
    if (!$this->exists($varName, $accessor)) {
103
      throw new \Exception("No [{$accessor}] accessor found for variable [{$varName}] on " . get_called_class() . "::" . __METHOD__);
104
    }
105
106
    $functionName = $varName . $accessor;
107
108
    if(isset($this->shared[$functionName])) {
109
      if(!array_key_exists($functionName, $this->executed)) {
110
        $this->executed[$functionName] = call_user_func_array($this->accessors[$functionName], $params);
111
      }
112
      return $this->executed[$functionName];
113
    }
114
115
    return call_user_func_array($this->accessors[$functionName], $params);
116
  }
117
118
}
119