Remove another Throwable abuse

This commit is contained in:
Dylan K. Taylor 2019-01-17 22:43:52 +00:00
parent 2dee7e9e0f
commit c5df2f6f0d
2 changed files with 40 additions and 37 deletions

View File

@ -49,6 +49,7 @@ class BanEntry{
public function __construct(string $name){
$this->name = strtolower($name);
/** @noinspection PhpUnhandledExceptionInspection */
$this->creationDate = new \DateTime();
}
@ -60,6 +61,11 @@ class BanEntry{
return $this->creationDate;
}
/**
* @param \DateTime $date
*
* @throws \InvalidArgumentException
*/
public function setCreated(\DateTime $date){
self::validateDate($date);
$this->creationDate = $date;
@ -82,6 +88,7 @@ class BanEntry{
/**
* @param \DateTime|null $date
* @throws \InvalidArgumentException
*/
public function setExpires(\DateTime $date = null){
if($date !== null){
@ -91,6 +98,7 @@ class BanEntry{
}
public function hasExpired() : bool{
/** @noinspection PhpUnhandledExceptionInspection */
$now = new \DateTime();
return $this->expirationDate === null ? false : $this->expirationDate < $now;
@ -127,10 +135,14 @@ class BanEntry{
*
* @param \DateTime $dateTime
*
* @throws \RuntimeException if the argument can't be parsed from a formatted date string
* @throws \InvalidArgumentException if the argument can't be parsed from a formatted date string
*/
private static function validateDate(\DateTime $dateTime) : void{
self::parseDate($dateTime->format(self::$format));
try{
self::parseDate($dateTime->format(self::$format));
}catch(\RuntimeException $e){
throw new \InvalidArgumentException($e->getMessage(), 0, $e);
}
}
/**
@ -142,7 +154,7 @@ class BanEntry{
private static function parseDate(string $date) : ?\DateTime{
$datetime = \DateTime::createFromFormat(self::$format, $date);
if(!($datetime instanceof \DateTime)){
throw new \RuntimeException("Error parsing date for BanEntry: " . implode(", ", \DateTime::getLastErrors()["errors"]));
throw new \RuntimeException("Corrupted date/time: " . implode(", ", \DateTime::getLastErrors()["errors"]));
}
return $datetime;
@ -157,36 +169,26 @@ class BanEntry{
public static function fromString(string $str) : ?BanEntry{
if(strlen($str) < 2){
return null;
}else{
$str = explode("|", trim($str));
$entry = new BanEntry(trim(array_shift($str)));
do{
if(empty($str)){
break;
}
$entry->setCreated(self::parseDate(array_shift($str)));
if(empty($str)){
break;
}
$entry->setSource(trim(array_shift($str)));
if(empty($str)){
break;
}
$expire = trim(array_shift($str));
if($expire !== "" and strtolower($expire) !== "forever"){
$entry->setExpires(self::parseDate($expire));
}
if(empty($str)){
break;
}
$entry->setReason(trim(array_shift($str)));
}while(false);
return $entry;
}
$parts = explode("|", trim($str));
$entry = new BanEntry(trim(array_shift($parts)));
if(!empty($parts)){
$entry->setCreated(self::parseDate(array_shift($parts)));
}
if(!empty($parts)){
$entry->setSource(trim(array_shift($parts)));
}
if(!empty($parts)){
$expire = trim(array_shift($parts));
if($expire !== "" and strtolower($expire) !== "forever"){
$entry->setExpires(self::parseDate($expire));
}
}
if(!empty($parts)){
$entry->setReason(trim(array_shift($parts)));
}
return $entry;
}
}

View File

@ -32,6 +32,7 @@ use function is_resource;
use function strftime;
use function strtolower;
use function time;
use function trim;
class BanList{
@ -156,14 +157,14 @@ class BanList{
if($line{0} !== "#"){
try{
$entry = BanEntry::fromString($line);
if($entry instanceof BanEntry){
if($entry !== null){
$this->list[$entry->getName()] = $entry;
}
}catch(\Throwable $e){
}catch(\RuntimeException $e){
$logger = \GlobalLogger::get();
$logger->critical("Failed to parse ban entry from string \"$line\": " . $e->getMessage());
$logger->logException($e);
$logger->critical("Failed to parse ban entry from string \"" . trim($line) . "\": " . $e->getMessage());
}
}
}
fclose($fp);