PtlVariableDecorator::decorate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
ccs 0
cts 1
cp 0
crap 2
1
<?php
2
/**
3
 * Created by Gorlum 05.02.2018 12:31
4
 */
5
6
namespace Ptl;
7
8
use PTLTag;
9
use \template;
0 ignored issues
show
Bug introduced by
The type \template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class PtlVariableDecorator {
12
  // Numeric decorators
13
  const PARAM_NUMERIC = 'num'; // define numeric decorator
14
  const PARAM_NUMERIC_FLOOR = 'floor';
15
  const PARAM_NUMERIC_CEIL = 'ceil';
16
  const PARAM_NUMERIC_ROUND = 'round'; // round=<decimal numbers>
17
18
  const PARAM_NUMERIC_FORMAT = 'format';
19
  const PARAM_NUMERIC_MONEY = 'money';
20
  const PARAM_NUMERIC_COLOR = 'color'; // Color values: red - negative, yellow - zero, green - positive. Implies "floor" and "format"
21
//  const PARAM_NUMERIC_LIMIT = 'percent'; // _number_color_value replacement - see Decorators in PTL test
22
//  const PARAM_NUMERIC_LIMIT = 'limit';
23
24
  const PARAM_DATETIME = 'datetime'; // define date and/or time decorator
25
26
  /**
27
   * Сортированный список поддерживаемых параметров
28
   *
29
   * @var string[] $allowedParams
30
   */
31
  protected static $allowedParams = array(
32
    self::PARAM_NUMERIC       => '',
33
    // Will be dumped for all tags which does not have |num
34
    self::PARAM_NUMERIC_CEIL  => self::PARAM_NUMERIC,
35
    self::PARAM_NUMERIC_FLOOR => self::PARAM_NUMERIC,
36
    self::PARAM_NUMERIC_ROUND => self::PARAM_NUMERIC,
37
38
    self::PARAM_NUMERIC_FORMAT => self::PARAM_NUMERIC,
39
    self::PARAM_NUMERIC_MONEY  => self::PARAM_NUMERIC,
40
    self::PARAM_NUMERIC_COLOR  => self::PARAM_NUMERIC,
41
//    self::PARAM_NUMERIC_LIMIT  => self::PARAM_NUMERIC,
42
43
    self::PARAM_DATETIME       => self::PARAM_DATETIME,
44
  );
45
46
  /**
47
   * @param string   $strTagFull - full PTL tag with enclosing curly braces
48
   * @param string   $phpCompiledVar - compiled var reference ready for ECHO command
49
   * @param template $template - template to apply
50
   *
51
   * @return mixed
52
   */
53
  public static function decorate($strTagFull, $phpCompiledVar, $template) {
54
    $ptlTag = new PTLTag(substr($strTagFull, 1, strlen($strTagFull) - 2), $template, static::$allowedParams);
55
56
    $phpCompiledVar = static::num($phpCompiledVar, $ptlTag);
57
    $phpCompiledVar = static::datetime($phpCompiledVar, $ptlTag);
58
59
    return $phpCompiledVar;
60
  }
61
62
  /**
63
   * Return function call
64
   *
65
   * @param string   $funcName
66
   * @param string   $value
67
   * @param string[] $params
68
   *
69
   * @return string
70
   */
71
  protected static function func($funcName, $value, $params = []) {
72
    return $funcName . '(' . $value . (!empty($params) ? ',' . implode(',', $params) : '') . ')';
73
  }
74
75
  protected static function func2($funcName, $params = []) {
76
    return $funcName . '(' . (!empty($params) ? implode(',', $params) : '') . ')';
77
  }
78
79
  /**
80
   * @param        $phpCompiledVar
81
   * @param PTLTag $ptlTag
82
   */
83
  protected static function num($phpCompiledVar, $ptlTag) {
84
    $result = $phpCompiledVar;
85
86
    if (array_key_exists(self::PARAM_NUMERIC, $ptlTag->params)) {
87
      // Just dump other params
88
      foreach (static::$allowedParams as $paramName => $limitTag) {
89
        if ($limitTag != self::PARAM_NUMERIC || !array_key_exists($paramName, $ptlTag->params)) {
90
          continue;
91
        }
92
93
        switch ($paramName) {
94
          case self::PARAM_NUMERIC_CEIL:
95
          case self::PARAM_NUMERIC_FLOOR:
96
            $result = static::func($paramName, $result, '');
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type string[] expected by parameter $params of Ptl\PtlVariableDecorator::func(). ( Ignorable by Annotation )

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

96
            $result = static::func($paramName, $result, /** @scrutinizer ignore-type */ '');
Loading history...
97
          break;
98
99
          case self::PARAM_NUMERIC_ROUND:
100
            $result = static::func($paramName, $result, [intval($ptlTag->params[$paramName])]);
101
          break;
102
103
          case self::PARAM_NUMERIC_FORMAT:
104
            $result = static::func('HelperString::numberFormat', $result, [0]);
105
          break;
106
107
          case self::PARAM_NUMERIC_MONEY:
108
            $result = static::func('HelperString::numberFormat', $result, [2]);
109
          break;
110
111
          case self::PARAM_NUMERIC_COLOR:
112
            $result = static::func('prettyNumberStyledDefault', $result);
113
          break;
114
        }
115
      }
116
117
    }
118
119
    return $result;
120
  }
121
122
  /**
123
   * @param        $phpCompiledVar
124
   * @param PTLTag $ptlTag
125
   */
126
  protected static function datetime($phpCompiledVar, $ptlTag) {
127
    $result = $phpCompiledVar;
128
129
    if (array_key_exists(self::PARAM_DATETIME, $ptlTag->params)) {
130
      // Just dump other params
131
      foreach (static::$allowedParams as $paramName => $limitTag) {
132
        if ($limitTag != self::PARAM_DATETIME || !array_key_exists($paramName, $ptlTag->params)) {
133
          continue;
134
        }
135
136
        switch ($paramName) {
137
          case self::PARAM_DATETIME:
138
            $result = "empty($result) ? '' : " . static::func2('date', ["'" . FMT_DATE_TIME_SQL . "'", $result]);
139
          break;
140
        }
141
      }
142
143
    }
144
145
    return $result;
146
  }
147
148
}
149