Passed
Push — master ( 17602b...453007 )
by Chris
04:37
created
php/hamle/Text/Func.php 1 patch
Braces   +55 added lines, -35 removed lines patch added patch discarded remove patch
@@ -50,10 +50,12 @@  discard block
 block discarded – undo
50 50
    */
51 51
   public function __construct($s) {
52 52
     $m = array();
53
-    if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m))
54
-      throw new ParseError("Unable to read \$ func in '$s'");
55
-    if (trim($m[2]))
56
-      $this->sub = new FuncSub($m[2]);
53
+    if (!preg_match('/^\$\((' . self::REGEX_FUNCSEL . '*)(.*)\)$/', $s, $m)) {
54
+          throw new ParseError("Unable to read \$ func in '$s'");
55
+    }
56
+    if (trim($m[2])) {
57
+          $this->sub = new FuncSub($m[2]);
58
+    }
57 59
     if (!trim($m[1])) {
58 60
       $this->scope = true;
59 61
       return;
@@ -70,16 +72,23 @@  discard block
 block discarded – undo
70 72
     $m = array();
71 73
     $att = array('id' => array(), 'tag' => array());
72 74
     foreach (explode(",", $s) as $str) {
73
-      if (preg_match('/^[a-zA-Z0-9\\_]+/', $str, $m)) $type = $m[0];
74
-      else $type = "*";
75
-      if (preg_match('/#([a-zA-Z0-9\_\\${}]+)/', $str, $m)) $att['id'][$type][] = $m[1];
76
-      elseif (preg_match_all('/\\.([a-zA-Z0-9\_\-\\${}]+)/', $str, $m))
77
-        foreach ($m[1] as $tag)
75
+      if (preg_match('/^[a-zA-Z0-9\\_]+/', $str, $m)) {
76
+        $type = $m[0];
77
+      } else {
78
+        $type = "*";
79
+      }
80
+      if (preg_match('/#([a-zA-Z0-9\_\\${}]+)/', $str, $m)) {
81
+        $att['id'][$type][] = $m[1];
82
+      } elseif (preg_match_all('/\\.([a-zA-Z0-9\_\-\\${}]+)/', $str, $m)) {
83
+              foreach ($m[1] as $tag)
78 84
           $att['tag'][$type][] = new Text($tag, Text::TOKEN_CODE);
79
-      else $att['tag'][$type] = array();
85
+      } else {
86
+        $att['tag'][$type] = array();
87
+      }
88
+    }
89
+    if (!(count($att['id']) xor count($att['tag']))) {
90
+          throw new ParseError("Only tag, type or id can be combined");
80 91
     }
81
-    if (!(count($att['id']) xor count($att['tag'])))
82
-      throw new ParseError("Only tag, type or id can be combined");
83 92
     return $att;
84 93
   }
85 94
 
@@ -92,14 +101,18 @@  discard block
 block discarded – undo
92 101
     }
93 102
     $rand = false;
94 103
     if (preg_match_all('/\\^(-?)([a-zA-Z0-9\_]*)/', $s, $m)) {
95
-      foreach($m[0] as $k=>$mv)
96
-        if ($m[2][$k]) {
104
+      foreach($m[0] as $k=>$mv) {
105
+              if ($m[2][$k]) {
97 106
           $dir = $m[1][$k] == "-"?Hamle\Hamle::SORT_DESCENDING:Hamle\Hamle::SORT_ASCENDING;
107
+      }
98 108
           $att['sort'][$m[2][$k]] = $dir;
99
-        } else $rand = true;
109
+        } else {
110
+          $rand = true;
111
+        }
112
+    }
113
+    if($rand) {
114
+          $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM];
100 115
     }
101
-    if($rand)
102
-      $att['sort'] = [""=>$att['dir'] = Hamle\Hamle::SORT_RANDOM];
103 116
     return $att;
104 117
   }
105 118
 
@@ -124,17 +137,19 @@  discard block
 block discarded – undo
124 137
     }
125 138
     $limit = Text::varToCode($this->sortlimit['sort']) . "," .
126 139
         $this->sortlimit['limit'] . "," . $this->sortlimit['offset'];
127
-    if (count($this->filt['tag']))
128
-      return "Hamle\\Run::modelTypeTags(" .
140
+    if (count($this->filt['tag'])) {
141
+          return "Hamle\\Run::modelTypeTags(" .
129 142
       Text::varToCode($this->filt['tag']) . ",$limit)$sub";
130
-    if (count($this->filt['id']))
131
-      if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1)
143
+    }
144
+    if (count($this->filt['id'])) {
145
+          if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) == 1)
132 146
         return "Hamle\\Run::modelId(" .
133 147
         Text::varToCode(current($this->filt['id']['*'])) .
134 148
         ",$limit)$sub";
135
-      else
136
-        return "Hamle\\Run::modelTypeId(" .
149
+    } else {
150
+              return "Hamle\\Run::modelTypeId(" .
137 151
         Text::varToCode($this->filt['id']) . ",$limit)$sub";
152
+      }
138 153
     return "";
139 154
   }
140 155
 
@@ -145,34 +160,39 @@  discard block
 block discarded – undo
145 160
   public function getOrCreateModel(Model $parent = null) {
146 161
     if($this->scope instanceof Scope) {
147 162
       $parent = $this->scope->getOrCreateModel();
148
-    } elseif ($this->scope === true)
149
-      $parent = \Seufert\Hamle\Scope::get(0);
150
-    if ($this->filt && count($this->filt['tag']))
151
-      $parent = \Seufert\Hamle\Run::modelTypeTags(
163
+    } elseif ($this->scope === true) {
164
+          $parent = \Seufert\Hamle\Scope::get(0);
165
+    }
166
+    if ($this->filt && count($this->filt['tag'])) {
167
+          $parent = \Seufert\Hamle\Run::modelTypeTags(
152 168
         $this->filt['tag'],
153 169
         $this->sortlimit['sort'],
154 170
         $this->sortlimit['limit'],
155 171
         $this->sortlimit['offset']
156 172
       );
157
-    if ($this->filt && count($this->filt['id']))
158
-      if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1)
173
+    }
174
+    if ($this->filt && count($this->filt['id'])) {
175
+          if (isset($this->filt['id']['*']) && count($this->filt['id']['*']) === 1)
159 176
         $parent = \Seufert\Hamle\Run::modelId(
160 177
           current($this->filt['id']['*']),
161 178
             $this->sortlimit['sort'],
162 179
             $this->sortlimit['limit'],
163 180
             $this->sortlimit['offset']
164 181
             );
165
-      else
166
-        $parent = \Seufert\Hamle\Run::modelTypeId(
182
+    } else {
183
+              $parent = \Seufert\Hamle\Run::modelTypeId(
167 184
           $this->filt['id'],
168 185
           $this->sortlimit['sort'],
169 186
           $this->sortlimit['limit'],
170 187
           $this->sortlimit['offset']
171 188
         );
172
-    if($this->sub)
173
-      return $this->sub->getOrCreateModel($parent);
174
-    if(!$parent)
175
-      throw new \RuntimeException('Unable to create model with no relation');
189
+      }
190
+    if($this->sub) {
191
+          return $this->sub->getOrCreateModel($parent);
192
+    }
193
+    if(!$parent) {
194
+          throw new \RuntimeException('Unable to create model with no relation');
195
+    }
176 196
     return $parent;
177 197
   }
178 198
 
Please login to merge, or discard this patch.