Skip to content

Commit 964e499

Browse files
committed
Merge pull request PHPIDS#28 from jayzeng/refactor/exception
Refactor/exception
2 parents 78dfbe2 + c7cb7bf commit 964e499

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

lib/IDS/Filter/Storage.php

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Storage
9191
*
9292
* @param object $init IDS_Init instance
9393
*
94-
* @throws Exception if unsupported filter type is given
94+
* @throws \InvalidArgumentException if unsupported filter type is given
9595
* @return void
9696
*/
9797
final public function __construct(Init $init)
@@ -105,20 +105,18 @@ final public function __construct(Init $init)
105105
$this->source = $init->getBasePath()
106106
. $init->config['General']['filter_path'];
107107

108-
if ($caching && $caching != 'none') {
108+
if ($caching && $caching !== 'none') {
109109
$this->cacheSettings = $init->config['Caching'];
110110
$this->cache = CacheFactory::factory($init, 'storage');
111111
}
112112

113113
switch ($type) {
114114
case 'xml':
115-
$this->getFilterFromXML();
116-
break;
115+
return $this->getFilterFromXML();
117116
case 'json':
118-
$this->getFilterFromJson();
119-
break;
117+
return $this->getFilterFromJson();
120118
default:
121-
throw new \Exception('Unsupported filter type.');
119+
throw new \InvalidArgumentException('Unsupported filter type.');
122120
}
123121
}
124122
}
@@ -188,7 +186,7 @@ private function isCached()
188186
* If caching mode is enabled the result will be cached to increase
189187
* the performance.
190188
*
191-
* @throws Exception if problems with fetching the XML data occur
189+
* @throws \RuntimeException if problems with fetching the XML data occur
192190
* @return object $this
193191
*/
194192
public function getFilterFromXML()
@@ -274,13 +272,10 @@ public function getFilterFromXML()
274272
$this->cache->setCache($data);
275273
}
276274

277-
} else {
278-
throw new \Exception(
279-
'SimpleXML not loaded.'
280-
);
275+
return $this;
281276
}
282277

283-
return $this;
278+
throw new \RuntimeException('SimpleXML is not loaded.');
284279
}
285280

286281
/**
@@ -290,7 +285,7 @@ public function getFilterFromXML()
290285
* If caching mode is enabled the result will be cached to increase
291286
* the performance.
292287
*
293-
* @throws Exception if problems with fetching the JSON data occur
288+
* @throws \RuntimeException if problems with fetching the JSON data occur
294289
* @return object $this
295290
*/
296291
public function getFilterFromJson()
@@ -311,17 +306,17 @@ public function getFilterFromJson()
311306
$filters = json_decode(file_get_contents($this->source));
312307
} else {
313308
throw new \RuntimeException(
314-
'JSON data could not be loaded.' .
315-
' Make sure you specified the correct path.'
316-
);
309+
'JSON data could not be loaded.' .
310+
' Make sure you specified the correct path.'
311+
);
317312
}
318313
}
319314

320315
if (!$filters) {
321316
throw new \RuntimeException(
322-
'JSON data could not be loaded.' .
323-
' Make sure you specified the correct path. ' . $this->source
324-
);
317+
'JSON data could not be loaded.' .
318+
' Make sure you specified the correct path. ' . $this->source
319+
);
325320
}
326321

327322
/*
@@ -345,22 +340,22 @@ public function getFilterFromJson()
345340
$filter['description'];
346341

347342
$this->addFilter(
348-
new \IDS\Filter(
349-
$id,
350-
$rule,
351-
$description,
352-
(array) $tags[0],
353-
(int) $impact
354-
)
355-
);
343+
new \IDS\Filter(
344+
$id,
345+
$rule,
346+
$description,
347+
(array) $tags[0],
348+
(int) $impact
349+
)
350+
);
356351

357352
$data[] = array(
358-
'id' => $id,
359-
'rule' => $rule,
360-
'impact' => $impact,
361-
'tags' => $tags,
362-
'description' => $description
363-
);
353+
'id' => $id,
354+
'rule' => $rule,
355+
'impact' => $impact,
356+
'tags' => $tags,
357+
'description' => $description
358+
);
364359
}
365360

366361
/*
@@ -370,13 +365,10 @@ public function getFilterFromJson()
370365
$this->cache->setCache($data);
371366
}
372367

373-
} else {
374-
throw new \RuntimeException(
375-
'ext/json not loaded.'
376-
);
368+
return $this;
377369
}
378370

379-
return $this;
371+
throw new \RuntimeException('json extension is not loaded.');
380372
}
381373
}
382374

lib/IDS/Init.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,13 @@ public static function init($configPath = null)
111111
* If all that tests succeed the base path will be returned as a string -
112112
* else null will be returned.
113113
*
114-
* @return string the base path or null
114+
* @return string|null the base path or null
115115
*/
116116
public function getBasePath()
117117
{
118-
return ((isset($this->config['General']['base_path'])
119-
&& $this->config['General']['base_path']
120-
&& isset($this->config['General']['use_base_path'])
121-
&& $this->config['General']['use_base_path'])
122-
? $this->config['General']['base_path'] : null);
118+
return (!empty($this->config['General']['base_path'])
119+
&& !empty($this->config['General']['use_base_path']))
120+
? $this->config['General']['base_path'] : null;
123121
}
124122

125123
/**

0 commit comments

Comments
 (0)