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){ public function __construct(string $name){
$this->name = strtolower($name); $this->name = strtolower($name);
/** @noinspection PhpUnhandledExceptionInspection */
$this->creationDate = new \DateTime(); $this->creationDate = new \DateTime();
} }
@ -60,6 +61,11 @@ class BanEntry{
return $this->creationDate; return $this->creationDate;
} }
/**
* @param \DateTime $date
*
* @throws \InvalidArgumentException
*/
public function setCreated(\DateTime $date){ public function setCreated(\DateTime $date){
self::validateDate($date); self::validateDate($date);
$this->creationDate = $date; $this->creationDate = $date;
@ -82,6 +88,7 @@ class BanEntry{
/** /**
* @param \DateTime|null $date * @param \DateTime|null $date
* @throws \InvalidArgumentException
*/ */
public function setExpires(\DateTime $date = null){ public function setExpires(\DateTime $date = null){
if($date !== null){ if($date !== null){
@ -91,6 +98,7 @@ class BanEntry{
} }
public function hasExpired() : bool{ public function hasExpired() : bool{
/** @noinspection PhpUnhandledExceptionInspection */
$now = new \DateTime(); $now = new \DateTime();
return $this->expirationDate === null ? false : $this->expirationDate < $now; return $this->expirationDate === null ? false : $this->expirationDate < $now;
@ -127,10 +135,14 @@ class BanEntry{
* *
* @param \DateTime $dateTime * @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{ private static function validateDate(\DateTime $dateTime) : void{
try{
self::parseDate($dateTime->format(self::$format)); 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{ private static function parseDate(string $date) : ?\DateTime{
$datetime = \DateTime::createFromFormat(self::$format, $date); $datetime = \DateTime::createFromFormat(self::$format, $date);
if(!($datetime instanceof \DateTime)){ 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; return $datetime;
@ -157,36 +169,26 @@ class BanEntry{
public static function fromString(string $str) : ?BanEntry{ public static function fromString(string $str) : ?BanEntry{
if(strlen($str) < 2){ if(strlen($str) < 2){
return null; 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))); $parts = explode("|", trim($str));
if(empty($str)){ $entry = new BanEntry(trim(array_shift($parts)));
break; if(!empty($parts)){
$entry->setCreated(self::parseDate(array_shift($parts)));
} }
if(!empty($parts)){
$entry->setSource(trim(array_shift($str))); $entry->setSource(trim(array_shift($parts)));
if(empty($str)){
break;
} }
if(!empty($parts)){
$expire = trim(array_shift($str)); $expire = trim(array_shift($parts));
if($expire !== "" and strtolower($expire) !== "forever"){ if($expire !== "" and strtolower($expire) !== "forever"){
$entry->setExpires(self::parseDate($expire)); $entry->setExpires(self::parseDate($expire));
} }
if(empty($str)){
break;
} }
if(!empty($parts)){
$entry->setReason(trim(array_shift($str))); $entry->setReason(trim(array_shift($parts)));
}while(false); }
return $entry; return $entry;
} }
} }
}

View File

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