Completed
Push — trunk ( f009e4...c8f413 )
by SuperNova.WS
06:07
created

PtlVariableDecorator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 94
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
D num() 0 33 10
A func() 0 2 2
A decorate() 0 6 1
1
<?php
2
/**
3
 * Created by Gorlum 05.02.2018 12:31
4
 */
5
6
namespace Ptl;
7
8
use PTLTag;
9
use \template;
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_COLOR = 'color'; // Color values: red - negative, yellow - zero, green - positive. Implies "floor" and "format"
20
//  const PARAM_NUMERIC_LIMIT = 'percent'; // _number_color_value replacement - see Decorators in PTL test
21
//  const PARAM_NUMERIC_LIMIT = 'limit';
22
23
  /**
24
   * Сортированный список поддерживаемых параметров
25
   *
26
   * @var string[] $allowedParams
27
   */
28
  protected static $allowedParams = array(
29
    self::PARAM_NUMERIC        => '',
30
    // Will be dumped for all tags which does not have |num
31
    self::PARAM_NUMERIC_CEIL   => self::PARAM_NUMERIC,
32
    self::PARAM_NUMERIC_FLOOR  => self::PARAM_NUMERIC,
33
    self::PARAM_NUMERIC_ROUND  => self::PARAM_NUMERIC,
34
35
    self::PARAM_NUMERIC_FORMAT => self::PARAM_NUMERIC,
36
    self::PARAM_NUMERIC_COLOR  => self::PARAM_NUMERIC,
37
//    self::PARAM_NUMERIC_LIMIT  => self::PARAM_NUMERIC,
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
38
  );
39
40
  /**
41
   * @param string   $strTagFull - full PTL tag with enclosing curly braces
42
   * @param string   $phpCompiledVar - compiled var reference ready for ECHO command
43
   * @param template $template - template to apply
44
   *
45
   * @return mixed
46
   */
47
  public static function decorate($strTagFull, $phpCompiledVar, $template) {
48
    $ptlTag = new PTLTag(substr($strTagFull, 1, strlen($strTagFull) - 2), $template, static::$allowedParams);
49
50
    $phpCompiledVar = static::num($phpCompiledVar, $ptlTag);
51
52
    return $phpCompiledVar;
53
  }
54
55
  /**
56
   * Return function call
57
   *
58
   * @param string   $funcName
59
   * @param string   $value
60
   * @param string[] $params
61
   *
62
   * @return string
63
   */
64
  protected static function func($funcName, $value, $params = []) {
65
    return $funcName . '(' . $value . (!empty($params) ? ',' . implode(',', $params) : '') . ')';
66
  }
67
68
  /**
69
   * @param        $phpCompiledVar
70
   * @param PTLTag $ptlTag
71
   */
72
  protected static function num($phpCompiledVar, $ptlTag) {
73
    $result = $phpCompiledVar;
74
75
    if (array_key_exists(self::PARAM_NUMERIC, $ptlTag->params)) {
76
      // Just dump other params
77
      foreach(static::$allowedParams as $paramName => $limitTag) {
78
        if ($limitTag != self::PARAM_NUMERIC || !array_key_exists($paramName, $ptlTag->params)) {
79
          continue;
80
        }
81
82
        switch ($paramName) {
83
          case self::PARAM_NUMERIC_CEIL:
84
          case self::PARAM_NUMERIC_FLOOR:
85
            $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

85
            $result = static::func($paramName, $result, /** @scrutinizer ignore-type */ '');
Loading history...
86
          break;
87
88
          case self::PARAM_NUMERIC_ROUND:
89
            $result = static::func($paramName, $result, [intval($ptlTag->params[$paramName])]);
90
          break;
91
92
          case self::PARAM_NUMERIC_FORMAT:
93
            $result = static::func('HelperString::numberFormat', $result, [0]);
94
          break;
95
96
          case self::PARAM_NUMERIC_COLOR:
97
            $result = static::func('prettyNumberStyledDefault', $result);
98
          break;
99
        }
100
      }
101
102
    }
103
104
    return $result;
105
  }
106
107
}
108