Completed
Push — master ( e25cba...17602b )
by Chris
01:54
created

Complex::getOrCreateModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\Text;
27
28
use http\Exception\RuntimeException;
29
use Seufert\Hamle\Model;
30
use Seufert\Hamle\Run;
31
use Seufert\Hamle\Text;
32
use Seufert\Hamle\Exception\ParseError;
33
use Seufert\Hamle\WriteModel;
34
35
class Complex extends Text {
36
  protected $func;
37
  protected $sel = null;
38
  protected $filter;
39
40
  function __construct($s) {
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...
41
    if(FALSE !== $pos = strpos($s,'|')) {
42
      $this->filter = new Filter(substr($s, $pos+1), $this);
43
      $s = substr($s,0,$pos);
44
    }
45
    $s = preg_split("/-[>!]/", $s);
46
    // if(count($s) == 1) $s = explode("-!",$s[0]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
47
    if (!$s[0]) throw new ParseError("Unable to parse Complex Expression");
48
    if ($s[0][1] == "(")
49
      $this->func = new Text\Func($s[0]);
50
    elseif ($s[0][1] == "[")
51
      $this->func = new Text\Scope($s[0]);
52
    else
53
      $this->func = new SimpleVar($s[0]);
54
    array_shift($s);
55
    $this->sel = $s;
56
  }
57
58
  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...
59
    if($escape)
60
      return "<?=htmlspecialchars(" .$this->toPHP() . ")?>";
61
    return "<?=" . $this->toPHP() . "?>";
62
  }
63
  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...
64
    return $this->filter?$this->filter->toPHP():$this->toPHPVar();
65
  }
66
  function toPHPVar() {
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...
67
    if ($this->sel) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sel of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
      $sel = array();
69
      foreach ($this->sel as $s)
70
        $sel[] = "hamleGet('$s')";
71
      return $this->func->toPHP() . "->" . implode('->', $sel);
72
    } else
73
      return $this->func->toPHP();
74
  }
75
76
  function getOrCreateModel(Model $parent = null) {
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...
77
    if($this->func instanceof Text\Scope)
78
      return $this->func->getOrCreateModel($parent);
79
    if($this->func instanceof Text\Func)
80
      return $this->func->getOrCreateModel($parent);
81
  }
82
83
  /**
84
   * @param $value
85
   * @return WriteModel
86
   */
87
  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...
88
    if(!$this->sel || count($this->sel) != 1)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sel of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89
      throw new \RuntimeException('Can only set values, when one var name is present');
90
    $model = $this->getOrCreateModel();
91
    if(!$model instanceof WriteModel)
92
      throw new \RuntimeException('Can only set values on Runtime Exceptions');
93
    $model->hamleSet($this->sel[0], $value);
94
    return $model;
95
  }
96
97
98
}