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

FuncSub::toPHP()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 Seufert\Hamle;
29
use Seufert\Hamle\Exception\ParseError;
30
use Seufert\Hamle\Model;
31
32
class FuncSub extends Hamle\Text\Func {
33
  protected $dir;
34
  protected $grouptype = ['grouptype' => 0];
35
36
  /**
37
   * FuncSub constructor.
38
   * @param string $s
39
   */
40
  public function __construct($s) {
41
    $m = array();
42 View Code Duplication
    if (!preg_match('/^ +([><]) +('.self::REGEX_FUNCSEL . '+)(.*)$/', $s, $m))
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...
43
      throw new ParseError("Unable to read \$ sub func in '$s'");
44
    if ($m[1] == "<") $this->dir = Hamle\Hamle::REL_PARENT;
45
    elseif ($m[1] == ">") $this->dir = Hamle\Hamle::REL_CHILD;
46
    else $this->dir = Hamle\Hamle::REL_ANY;
47
    $this->sortlimit = $this->attSortLimit($m[2]);
48
    $this->filt = $this->attIdTag($m[2]);
49
    $this->grouptype = $this->attGroupType($m[2]);
50
    if ($this->filt['id']) throw new ParseError("Unable to select by id");
51
    if (trim($m[3]))
52
      $this->sub = new FuncSub($m[3]);
53
  }
54
55
  /**
56
   * Return as PHP Code
57
   * @return string
58
   */
59
  public function toPHP() {
60
    $limit = Hamle\Text::varToCode($this->sortlimit['sort']) . "," .
61
        $this->sortlimit['limit'] . "," . $this->sortlimit['offset'] . "," .
62
        $this->grouptype['grouptype'];
63
    $sub = $this->sub ? "->" . $this->sub->toPHP() : "";
64
    return "hamleRel(" . $this->dir . "," .
65
    Hamle\Text::varToCode($this->filt['tag']) . ",$limit)$sub";
66
  }
67
68
  public function getOrCreateModel(Model $parent = null) {
69
    $model = $parent->hamleRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'],
0 ignored issues
show
Bug introduced by
It seems like $parent is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
70
      $this->sortlimit['limit'], $this->sortlimit['offset']);
71
    if(!$model->valid()) {
72
      if(!$parent instanceof Hamle\WriteModel)
73
        throw new \Exception('Cant create model, ' . get_class($parent) . ' must implement Hamle\\WriteModel.');
74
      $model = $parent->hamleCreateRel($this->dir, $this->filt['tag'], $this->sortlimit['sort'],
75
        $this->sortlimit['limit'], $this->sortlimit['offset']);
76
    }
77
    if($this->sub)
78
      return $this->sub->getOrCreateModel($model);
79
    return $model;
80
  }
81
82
}