Completed
Push — master ( 063e7f...e25cba )
by Chris
02:38
created

Text   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 99
Duplicated Lines 6.06 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 6
loc 99
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 6 33 13
A toHTML() 0 6 2
A toHTMLAtt() 0 3 1
A toPHP() 0 6 2
A doEval() 0 3 1
B varToCode() 0 16 9
A setValue() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
This project is Licenced under The MIT License (MIT)
4
5
Copyright (c) 2014 Christopher Seufert
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
THE SOFTWARE.
24
25
 */
26
namespace Seufert\Hamle;
27
28
/**
29
 * HAMLE String Conversion Library
30
 *
31
 * @author Chris Seufert <[email protected]>
32
 * @package hamle
33
 */
34
class Text {
35
  const TOKEN_CONTROL = 0x07;
36
  const TOKEN_HTML = 0x06;
37
  const TOKEN_CODE = 0x04;
38
39
  const REGEX_HTML = '/(\\$[a-zA-Z_][a-zA-Z0-9_]*)|({\\$.*?})/';
40
  const REGEX_CODE = '//';
41
42
  const FIND_DOLLARFUNC = 0x01;
43
  const FIND_DOLLARVAR = 0x02;
44
  const FIND_BARDOLLAR = 0x04;
45
46
  /**
47
   * @var \Seufert\Hamle\Text[] Array of Child String Objects
48
   */
49
  protected $nodes;
50
51
  function __construct($s, $mode = self::TOKEN_HTML) {
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...
52
    $m = array();
53
    $pos = 0;
54
    $this->nodes = array();
55
    $rFlag = PREG_OFFSET_CAPTURE + PREG_SET_ORDER;
56
    if (strlen(trim($s)) == 0) return;
57
    if ($mode == self::TOKEN_CONTROL) {
58
      if (preg_match('/^"(.*)"$/', trim($s), $m)) {
59
        $this->nodes[] = new Text($m[1]);
60
      } else
61
        $this->nodes[] = new Text\Complex(trim($s));
62
      return;
63
    }
64
    preg_match_all(self::REGEX_HTML, $s, $m, $rFlag);
65
    foreach ($m as $match) {
0 ignored issues
show
Bug introduced by
The expression $m of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
66
      if ($mode & self::FIND_BARDOLLAR && isset($match[2])) {
67 View Code Duplication
        if ($match[2][1] != $pos)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
          $this->nodes[] = new Text\Plain(
69
              substr($s, $pos, $match[2][1] - $pos), $mode);
70
        $this->nodes[] = new Text\Complex(substr($match[2][0], 1, -1));
71
        $pos = $match[2][1] + strlen($match[2][0]);
72
      } elseif ($mode & self::FIND_DOLLARVAR) {
73
        if ($match[1][1] > 0 && $s[$match[1][1] - 1] == '\\') continue;
74 View Code Duplication
        if ($match[1][1] != $pos)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
          $this->nodes[] = new Text\Plain(
76
              substr($s, $pos, $match[1][1] - $pos), $mode);
77
        $this->nodes[] = new Text\SimpleVar($match[1][0]);
78
        $pos = $match[1][1] + strlen($match[1][0]);
79
      }
80
    }
81
    if ($pos != strlen($s))
82
      $this->nodes[] = new Text\Plain(substr($s, $pos), $mode);
83
  }
84
85
  function toHTML($escape = false) {
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...
86
    $out = array();
87
    foreach ($this->nodes as $string)
88
      $out[] = $string->toHTML($escape);
89
    return implode("", $out);
90
  }
91
92
  function toHTMLAtt() {
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...
93
    return $this->toHtml(true);
94
  }
95
96
  function toPHP() {
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...
97
    $out = array();
98
    foreach ($this->nodes as $string)
99
      $out[] = $string->toPHP();
100
    return implode(".", $out);
101
  }
102
103
  function doEval() {
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...
104
    return eval('use Seufert\Hamle; return ' . $this->toPHP() . ';');
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
105
  }
106
107
  static function varToCode($var) {
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...
108
    if (is_array($var)) {
109
      $code = array();
110
      foreach ($var as $key => $value)
111
        $code[] = self::varToCode($key) . "=>" . self::varToCode($value);
112
      return 'array(' . implode(",", $code) . ')'; //remove unnecessary coma
113
    } elseif (is_bool($var)) {
114
      return ($var ? 'TRUE' : 'FALSE');
115
    } elseif (is_int($var) || is_float($var) || is_numeric($var)) {
116
      return $var;
117
    } elseif ($var instanceof Text) {
118
      return $var->toPHP();
119
    } else {
120
      return "'" . str_replace(array('$', "'"), array('\\$', "\\'"), $var) . "'";
121
    }
122
  }
123
124
  /**
125
   * @param $value
126
   * @return WriteModel
127
   */
128
  function setValue($value) {
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...
129
    throw new \RuntimeException('Unsupported');
130
  }
131
132
}
133
134