1 | <?php |
||||
2 | |||||
3 | namespace Phpsa\Datastore; |
||||
4 | |||||
5 | use Phpsa\Datastore\Datastore; |
||||
6 | use Phpsa\Datastore\Asset; |
||||
7 | use Phpsa\Datastore\DatastoreException; |
||||
8 | |||||
9 | class Helpers { |
||||
10 | |||||
11 | /** |
||||
12 | * tests if teh array is an associated array or an indexed array |
||||
13 | * |
||||
14 | * @param array $array |
||||
15 | * |
||||
16 | * @return bool |
||||
17 | */ |
||||
18 | public static function isAssocArray(array $array){ |
||||
19 | if (array() === $array) return false; |
||||
20 | return array_keys($array) !== range(0, count($array) - 1); |
||||
21 | } |
||||
22 | |||||
23 | /** |
||||
24 | * Splits a word by capitals and glues it together with a space or the glue |
||||
25 | * |
||||
26 | * @param string $string |
||||
27 | * @param boolean $ucfirst |
||||
28 | * @param string $glue |
||||
29 | * @return string |
||||
30 | */ |
||||
31 | public static function splitByCaps($string, $ucfirst = true, $glue = false) |
||||
32 | { |
||||
33 | $pattern = "/(.)([A-Z])/"; |
||||
34 | $replacement = "\\1 \\2"; |
||||
35 | $return = ($ucfirst) ? |
||||
36 | ucfirst(preg_replace($pattern, $replacement, $string)) : |
||||
37 | strtolower(preg_replace($pattern, $replacement, $string)); |
||||
38 | |||||
39 | return ($glue) ? str_replace(' ', $glue, $return) : $return; |
||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * PArse out the asset type to the class namespaced version. |
||||
44 | * |
||||
45 | * @param [type] $queryString |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||||
46 | * @param bool $key |
||||
47 | * |
||||
48 | * @return void |
||||
49 | */ |
||||
50 | public static function parseAssetType($queryString, $key = false){ |
||||
51 | $assetParts = explode(".", strtolower($queryString)); |
||||
52 | if(count($assetParts) > 2){ |
||||
53 | throw new DatastoreException("Assets are only 2 tiers deep"); |
||||
54 | } |
||||
55 | $asset = array_pop($assetParts); |
||||
56 | $list = self::getAssetList(true); |
||||
0 ignored issues
–
show
Are you sure the assignment to
$list is correct as self::getAssetList(true) targeting Phpsa\Datastore\Helpers::getAssetList() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
57 | |||||
58 | $list = !empty($assetParts) && isset($list[$assetParts[0]]) ? $list[$assetParts[0]] : array_shift($list); |
||||
59 | |||||
60 | if(!isset($list[$asset])){ |
||||
61 | throw new DatastoreException("Asset not found"); |
||||
62 | } |
||||
63 | return $key ? $list[$asset]['class'] : $list[$asset]; |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * Gets the asset definition |
||||
68 | * |
||||
69 | * @param [type] $className |
||||
0 ignored issues
–
show
|
|||||
70 | * |
||||
71 | * @return void |
||||
72 | */ |
||||
73 | public static function getAssetItem($className){ |
||||
74 | return array( |
||||
0 ignored issues
–
show
|
|||||
75 | 'class' => $className, |
||||
76 | 'name' => Self::assetInfo($className, 'name'), |
||||
77 | 'name_singular' => Self::assetInfo($className, 'name_singular'), |
||||
78 | 'shortname' => Self::assetInfo($className, 'shortname'), |
||||
79 | 'icon' => Self::assetInfo($className, 'icon'), |
||||
80 | 'children' => Self::assetInfo($className, 'children'), |
||||
81 | 'is_child' => Self::assetInfo($className, 'is_child'), |
||||
82 | 'max_instances' => Self::assetInfo($className, 'max_instances'), |
||||
83 | 'about' => self::callStatic($className, 'about'), |
||||
84 | 'private' => Self::assetInfo($className, 'private'), |
||||
85 | 'has_meta' => Self::assetInfo($className, 'meta_description') !== 'off' && Self::assetInfo($className, 'meta_keywords') !== 'off', |
||||
86 | 'status_equals' => Self::assetInfo($className, 'status_equals') |
||||
87 | ); |
||||
88 | } |
||||
89 | |||||
90 | public static function getStatusEquals($className){ |
||||
91 | $statusEquals = self::assetInfo($className, 'status_equals'); |
||||
92 | if(!$statusEquals){ |
||||
93 | return null; |
||||
94 | } |
||||
95 | $props = self::getAssetProps($className,$statusEquals); |
||||
96 | return isset($props['published'])?$props['published'] : null; |
||||
97 | } |
||||
98 | |||||
99 | |||||
100 | public static function getAssetProps($className, $property = null){ |
||||
101 | $properties = self::assetInfo($className, 'properties'); |
||||
102 | if(!$properties){ |
||||
103 | throw new DatastoreException("No Properties found for asset"); |
||||
104 | } |
||||
105 | if($property){ |
||||
106 | return isset($properties[$property]) ? $properties[$property] : null; |
||||
107 | } |
||||
108 | return $properties; |
||||
109 | } |
||||
110 | |||||
111 | /** |
||||
112 | * List of assets |
||||
113 | * |
||||
114 | * @param bool $grouped groupd by type |
||||
115 | * @param bool $includeChildren includ child assets |
||||
116 | * |
||||
117 | * @return void |
||||
118 | */ |
||||
119 | public static function getAssetList($grouped = false, $includeChildren = true) |
||||
120 | { |
||||
121 | $assets = array(); |
||||
122 | $datastoreAssets = config("datastore.assets"); |
||||
123 | foreach($datastoreAssets as $className){ |
||||
124 | |||||
125 | $asset = self::getAssetItem($className); |
||||
0 ignored issues
–
show
Are you sure the assignment to
$asset is correct as self::getAssetItem($className) targeting Phpsa\Datastore\Helpers::getAssetItem() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
126 | |||||
127 | if (self::assetNamespace($className) !== 'asset') { |
||||
128 | throw new DatastoreException('Only assets of type assets should be used ' . $className); |
||||
129 | } |
||||
130 | |||||
131 | if(!$includeChildren && $asset['is_child']){ |
||||
132 | continue; |
||||
133 | } |
||||
134 | |||||
135 | if($asset['children'] && $includeChildren && !in_array($asset['children'], $datastoreAssets)){ |
||||
136 | $child = self::getAssetItem($asset['children']); |
||||
0 ignored issues
–
show
Are you sure the assignment to
$child is correct as self::getAssetItem($asset['children']) targeting Phpsa\Datastore\Helpers::getAssetItem() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
137 | if (self::assetNamespace($child['class']) !== 'asset') { |
||||
138 | throw new DatastoreException('Only assets of type assets should be used ' . $child['class']); |
||||
139 | } |
||||
140 | |||||
141 | if ($grouped) |
||||
142 | { |
||||
143 | $mod = Helpers::getModule($child['class']); |
||||
144 | $assets[strtolower($mod)][strtolower($child['shortname'])] = $child; |
||||
145 | } |
||||
146 | else |
||||
147 | { |
||||
148 | $assets[strtolower($child['shortname'])] = $child; |
||||
149 | } |
||||
150 | } |
||||
151 | |||||
152 | |||||
153 | if ($grouped) |
||||
154 | { |
||||
155 | $mod = Helpers::getModule($className); |
||||
156 | $assets[strtolower($mod)][strtolower($asset['shortname'])] = $asset; |
||||
157 | } |
||||
158 | else |
||||
159 | { |
||||
160 | $assets[strtolower($asset['shortname'])] = $asset; |
||||
161 | } |
||||
162 | |||||
163 | |||||
164 | } |
||||
165 | |||||
166 | ksort($assets); |
||||
167 | if($grouped){ |
||||
168 | foreach($assets as &$group){ |
||||
169 | uasort($group, function ($a , $b) { |
||||
170 | return strcmp($a['name'], $b['name']); |
||||
171 | }); |
||||
172 | } |
||||
173 | } |
||||
174 | |||||
175 | return $assets; |
||||
176 | } |
||||
177 | |||||
178 | |||||
179 | /** |
||||
180 | * Gets the module based on the classname |
||||
181 | * @param string $class |
||||
182 | * @return string |
||||
183 | */ |
||||
184 | public static function getModule($class) |
||||
185 | { |
||||
186 | $parts = explode('\\', $class); |
||||
187 | array_pop($parts); |
||||
188 | $ns = ''; |
||||
189 | if(end($parts) !== 'Ams'){ |
||||
190 | $ns = ucfirst(array_pop($parts)); |
||||
191 | } |
||||
192 | |||||
193 | return $ns; |
||||
194 | } |
||||
195 | |||||
196 | /** |
||||
197 | * Gets the namespace of an asset! |
||||
198 | * @param string $className |
||||
199 | * @return string |
||||
200 | */ |
||||
201 | public static function assetNamespace($className) |
||||
202 | { |
||||
203 | return self::callStatic($className, 'getNamespace'); |
||||
204 | } |
||||
205 | |||||
206 | /** |
||||
207 | * gets the url path for the asset |
||||
208 | * |
||||
209 | * @param [type] $className |
||||
0 ignored issues
–
show
|
|||||
210 | * |
||||
211 | * @return void |
||||
212 | * @author Craig Smith <[email protected]> |
||||
213 | */ |
||||
214 | public static function getPath($className){ |
||||
215 | $mod = self::getModule($className); |
||||
216 | $sn = Self::assetInfo($className, 'shortname'); |
||||
217 | return !empty($mod) ? strtolower($mod .'.' . $sn) : strtolower($sn); |
||||
0 ignored issues
–
show
|
|||||
218 | } |
||||
219 | |||||
220 | /** |
||||
221 | * Calls a static method |
||||
222 | * @param string $classname class to call |
||||
223 | * @param string $method method to call |
||||
224 | * @param array $params_array method parameters |
||||
225 | * @return mixed |
||||
226 | */ |
||||
227 | public static function callStatic($classname, $method, $params_array = array()) |
||||
228 | { |
||||
229 | return call_user_func_array(array($classname, $method), $params_array); |
||||
230 | } |
||||
231 | |||||
232 | /** |
||||
233 | * gets information tag from the asset |
||||
234 | * @param string $classname |
||||
235 | * @param mixed $lookup - what information to lookup |
||||
236 | * @return mixed information |
||||
237 | */ |
||||
238 | public static function assetInfo($classname, $lookup = false) |
||||
239 | { |
||||
240 | return self::callStatic($classname, 'getinfo', array($lookup)); |
||||
241 | } |
||||
242 | |||||
243 | |||||
244 | /** |
||||
245 | * gets the classname from the path of the file |
||||
246 | * @param string $file |
||||
247 | * @return string |
||||
248 | */ |
||||
249 | public static function getClassnameFromPath($file) |
||||
250 | { |
||||
251 | if (!is_file(FCPATH . $file)) |
||||
0 ignored issues
–
show
|
|||||
252 | { |
||||
253 | return false; |
||||
0 ignored issues
–
show
|
|||||
254 | } |
||||
255 | $fp = fopen(FCPATH . $file, 'r'); |
||||
256 | |||||
257 | $class = $buffer = ''; |
||||
258 | $i = 0; |
||||
259 | while (!$class) |
||||
260 | { |
||||
261 | if (feof($fp)) |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of feof() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
262 | break; |
||||
263 | |||||
264 | $buffer .= fread($fp, 512); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of fread() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
265 | $tokens = token_get_all($buffer); |
||||
266 | |||||
267 | if (strpos($buffer, '{') === false) |
||||
268 | continue; |
||||
269 | |||||
270 | for (; $i < count($tokens); $i++) |
||||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||||
271 | { |
||||
272 | if ($tokens[$i][0] === T_CLASS) |
||||
273 | { |
||||
274 | for ($j = $i + 1; $j < count($tokens); $j++) |
||||
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||||
275 | { |
||||
276 | if ($tokens[$j] === '{') |
||||
277 | { |
||||
278 | $class = $tokens[$i + 2][1]; |
||||
279 | } |
||||
280 | } |
||||
281 | } |
||||
282 | } |
||||
283 | } |
||||
284 | return $class; |
||||
285 | } |
||||
286 | } |