mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Exceptions > create instance of exception using static "create()" method (instead of constructor)
This commit is contained in:
@@ -21,21 +21,22 @@ use Meritoo\Common\Utilities\Arrays;
|
||||
abstract class UnknownTypeException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string|int $unknownType The unknown type of something (value of constant)
|
||||
* @param BaseType $typeInstance An instance of class that contains type of the something
|
||||
* @param string $typeName Name of the something
|
||||
* @return UnknownTypeException
|
||||
*/
|
||||
public function __construct($unknownType, BaseType $typeInstance, $typeName)
|
||||
public static function create($unknownType, BaseType $typeInstance, $typeName)
|
||||
{
|
||||
$allTypes = $typeInstance->getAll();
|
||||
$types = Arrays::values2string($allTypes, '', ', ');
|
||||
|
||||
$template = 'The \'%s\' type of %s is unknown. Probably doesn\'t exist or there is a typo. You should use one'
|
||||
. ' of these types: %s.';
|
||||
|
||||
$allTypes = $typeInstance->getAll();
|
||||
$types = Arrays::values2string($allTypes, '', ', ');
|
||||
$message = sprintf(sprintf($template, $unknownType, $typeName, $types));
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,16 +19,18 @@ use Exception;
|
||||
class IncorrectBundleNameException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $bundleName Incorrect name of bundle
|
||||
* @return IncorrectBundleNameException
|
||||
*/
|
||||
public function __construct($bundleName)
|
||||
public static function create($bundleName)
|
||||
{
|
||||
$template = 'Name of bundle \'%s\' is incorrect. It should start with big letter and end with "Bundle". Is'
|
||||
. ' there everything ok?';
|
||||
|
||||
$message = sprintf($template, $bundleName);
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,16 @@ namespace Meritoo\Common\Exception\File;
|
||||
class EmptyFileException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $emptyFilePath Path of the empty file
|
||||
* @return EmptyFileException
|
||||
*/
|
||||
public function __construct($emptyFilePath)
|
||||
public static function create($emptyFilePath)
|
||||
{
|
||||
$template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?';
|
||||
$message = sprintf($template, $emptyFilePath);
|
||||
|
||||
parent::__construct($message);
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ namespace Meritoo\Common\Exception\File;
|
||||
class EmptyFilePathException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*/
|
||||
public function __construct()
|
||||
public static function create()
|
||||
{
|
||||
parent::__construct('Path of the file is empty. Did you provide path of proper file?');
|
||||
return new static('Path of the file is empty. Did you provide path of proper file?');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,16 @@ namespace Meritoo\Common\Exception\File;
|
||||
class NotExistingFileException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $notExistingFilePath Path of not existing (or not readable) file
|
||||
* @return NotExistingFileException
|
||||
*/
|
||||
public function __construct($notExistingFilePath)
|
||||
public static function create($notExistingFilePath)
|
||||
{
|
||||
$template = 'File with path \'%s\' does not exist (or is not readable). Did you provide path of proper file?';
|
||||
$message = sprintf($template, $notExistingFilePath);
|
||||
|
||||
parent::__construct($message);
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,13 @@ use Exception;
|
||||
class DisabledMethodException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $disabledMethod Name of the disabled method
|
||||
* @param string $alternativeMethod (optional) Name of the alternative method
|
||||
* @return DisabledMethodException
|
||||
*/
|
||||
public function __construct($disabledMethod, $alternativeMethod = '')
|
||||
public static function create($disabledMethod, $alternativeMethod = '')
|
||||
{
|
||||
$template = 'Method %s() cannot be called, because is disabled.';
|
||||
|
||||
@@ -33,6 +34,7 @@ class DisabledMethodException extends Exception
|
||||
}
|
||||
|
||||
$message = sprintf($template, $disabledMethod, $alternativeMethod);
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,15 @@ use Exception;
|
||||
class CannotResolveClassNameException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param array|object|string $source Source of the class's / trait's name. It can be an array of objects,
|
||||
* namespaces, object or namespace.
|
||||
* @param bool $forClass (optional) If is set to true, message of this exception for class is
|
||||
* prepared. Otherwise - for trait.
|
||||
* @return CannotResolveClassNameException
|
||||
*/
|
||||
public function __construct($source, $forClass = true)
|
||||
public static function create($source, $forClass = true)
|
||||
{
|
||||
$forWho = 'trait';
|
||||
$value = '';
|
||||
@@ -42,6 +43,6 @@ class CannotResolveClassNameException extends Exception
|
||||
$template = 'Name of %s from given \'%s\'%s cannot be resolved. Is there everything ok?';
|
||||
$message = sprintf($template, $forWho, gettype($source), $value);
|
||||
|
||||
parent::__construct($message);
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,12 +20,13 @@ use Meritoo\Common\Utilities\Reflection;
|
||||
class MissingChildClassesException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param array|object|string $parentClass Class that hasn't child classes, but it should. An array of objects,
|
||||
* strings, object or string.
|
||||
* @return MissingChildClassesException
|
||||
*/
|
||||
public function __construct($parentClass)
|
||||
public static function create($parentClass)
|
||||
{
|
||||
$template = 'The \'%s\' class requires one child class at least who will extend her (maybe is an abstract'
|
||||
. ' class), but the child classes are missing. Did you forget to extend this class?';
|
||||
@@ -33,6 +34,6 @@ class MissingChildClassesException extends Exception
|
||||
$parentClassName = Reflection::getClassName($parentClass);
|
||||
$message = sprintf($template, $parentClassName);
|
||||
|
||||
parent::__construct($message);
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,14 @@ use Meritoo\Common\Utilities\Reflection;
|
||||
class TooManyChildClassesException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array
|
||||
* of objects, strings, object or string.
|
||||
* @param array $childClasses Child classes
|
||||
* @return TooManyChildClassesException
|
||||
*/
|
||||
public function __construct($parentClass, array $childClasses)
|
||||
public static function create($parentClass, array $childClasses)
|
||||
{
|
||||
$template = "The '%s' class requires one child class at most who will extend her, but more than one child"
|
||||
. " class was found:\n- %s\n\nWhy did you create more than one classes that extend '%s' class?";
|
||||
@@ -34,6 +35,6 @@ class TooManyChildClassesException extends Exception
|
||||
$parentClassName = Reflection::getClassName($parentClass);
|
||||
$message = sprintf($template, $parentClassName, implode("\n- ", $childClasses), $parentClassName);
|
||||
|
||||
parent::__construct($message);
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,18 @@ namespace Meritoo\Common\Exception\Regex;
|
||||
class IncorrectColorHexLengthException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $color Incorrect hexadecimal value of color
|
||||
* @return IncorrectColorHexLengthException
|
||||
*/
|
||||
public function __construct($color)
|
||||
public static function create($color)
|
||||
{
|
||||
$template = 'Length of hexadecimal value of color \'%s\' is incorrect. It\'s %d, but it should be 3 or 6.'
|
||||
. ' Is there everything ok?';
|
||||
|
||||
$message = sprintf($template, $color, strlen($color));
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,15 @@ namespace Meritoo\Common\Exception\Regex;
|
||||
class InvalidColorHexValueException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $color Invalid hexadecimal value of color
|
||||
* @return InvalidColorHexValueException
|
||||
*/
|
||||
public function __construct($color)
|
||||
public static function create($color)
|
||||
{
|
||||
$message = sprintf('Hexadecimal value of color \'%s\' is invalid. Is there everything ok?', $color);
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,15 @@ namespace Meritoo\Common\Exception\Regex;
|
||||
class InvalidHtmlAttributesException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $htmlAttributes Invalid html attributes
|
||||
* @return InvalidHtmlAttributesException
|
||||
*/
|
||||
public function __construct($htmlAttributes)
|
||||
public static function create($htmlAttributes)
|
||||
{
|
||||
$message = sprintf('HTML attributes \'%s\' are invalid. Is there everything ok?', $htmlAttributes);
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception\Regex;
|
||||
|
||||
/**
|
||||
@@ -11,13 +17,15 @@ namespace Meritoo\Common\Exception\Regex;
|
||||
class InvalidUrlException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $url Invalid url
|
||||
* @return InvalidUrlException
|
||||
*/
|
||||
public function __construct($url)
|
||||
public static function create($url)
|
||||
{
|
||||
$message = sprintf('Url \'%s\' is invalid. Is there everything ok?', $url);
|
||||
parent::__construct($message);
|
||||
|
||||
return new static($message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception\Date;
|
||||
namespace Meritoo\Common\Exception\Type;
|
||||
|
||||
use Meritoo\Common\Exception\Base\UnknownTypeException;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
@@ -20,13 +20,17 @@ use Meritoo\Common\Type\DatePartType;
|
||||
class UnknownDatePartTypeException extends UnknownTypeException
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $unknownDatePart Type of date part, e.g. "year". One of DatePartType class constants.
|
||||
* @param string $unknownDatePart Unknown type of date part
|
||||
* @param string $value Incorrect value
|
||||
* @return UnknownDatePartTypeException
|
||||
*/
|
||||
public function __construct($unknownDatePart, $value)
|
||||
public static function createException($unknownDatePart, $value)
|
||||
{
|
||||
parent::__construct($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value));
|
||||
/* @var UnknownDatePartTypeException $exception */
|
||||
$exception = parent::create($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value));
|
||||
|
||||
return $exception;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception\Type;
|
||||
|
||||
use Meritoo\Common\Exception\Base\UnknownTypeException;
|
||||
@@ -14,10 +20,16 @@ use Meritoo\Common\Type\OopVisibilityType;
|
||||
class UnknownOopVisibilityTypeException extends UnknownTypeException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Creates exception
|
||||
*
|
||||
* @param string $unknownType Unknown visibility of a property, a method or (as of PHP 7.1.0) a constant
|
||||
* @return UnknownOopVisibilityTypeException
|
||||
*/
|
||||
public function __construct($unknownType)
|
||||
public static function createException($unknownType)
|
||||
{
|
||||
parent::__construct($unknownType, new OopVisibilityType(), 'OOP-related visibility');
|
||||
/* @var UnknownOopVisibilityTypeException $exception */
|
||||
$exception = parent::create($unknownType, new OopVisibilityType(), 'OOP-related visibility');
|
||||
|
||||
return $exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class Bundle
|
||||
* Given name of bundle is invalid?
|
||||
*/
|
||||
if (!Regex::isValidBundleName($bundleName)) {
|
||||
throw new IncorrectBundleNameException($bundleName);
|
||||
throw IncorrectBundleNameException::create($bundleName);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Meritoo\Common\Utilities;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Exception\Type\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
|
||||
@@ -255,21 +255,21 @@ class Date
|
||||
* Oops, incorrect year
|
||||
*/
|
||||
if ($year <= 0) {
|
||||
throw new UnknownDatePartTypeException(DatePartType::YEAR, $year);
|
||||
throw UnknownDatePartTypeException::createException(DatePartType::YEAR, $year);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, incorrect month
|
||||
*/
|
||||
if ($month < 1 || $month > 12) {
|
||||
throw new UnknownDatePartTypeException(DatePartType::MONTH, $month);
|
||||
throw UnknownDatePartTypeException::createException(DatePartType::MONTH, $month);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, incorrect day
|
||||
*/
|
||||
if ($day < 1 || $day > 31) {
|
||||
throw new UnknownDatePartTypeException(DatePartType::DAY, $day);
|
||||
throw UnknownDatePartTypeException::createException(DatePartType::DAY, $day);
|
||||
}
|
||||
|
||||
if ($month < 3) {
|
||||
|
||||
@@ -524,7 +524,7 @@ class Reflection
|
||||
* Oops, cannot resolve class
|
||||
*/
|
||||
if (null === $className) {
|
||||
throw new CannotResolveClassNameException($class);
|
||||
throw CannotResolveClassNameException::create($class);
|
||||
}
|
||||
|
||||
$childClasses = [];
|
||||
@@ -558,7 +558,6 @@ class Reflection
|
||||
*
|
||||
* @param array|object|string $parentClass Class who child class should be returned. An array of objects,
|
||||
* namespaces, object or namespace.
|
||||
* @throws CannotResolveClassNameException
|
||||
* @throws MissingChildClassesException
|
||||
* @throws TooManyChildClassesException
|
||||
* @return mixed
|
||||
@@ -572,7 +571,7 @@ class Reflection
|
||||
* Oops, the base / parent class hasn't child class
|
||||
*/
|
||||
if (empty($childClasses)) {
|
||||
throw new MissingChildClassesException($parentClass);
|
||||
throw MissingChildClassesException::create($parentClass);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -580,7 +579,7 @@ class Reflection
|
||||
* Oops, the base / parent class has too many child classes
|
||||
*/
|
||||
if (count($childClasses) > 1) {
|
||||
throw new TooManyChildClassesException($parentClass, $childClasses);
|
||||
throw TooManyChildClassesException::create($parentClass, $childClasses);
|
||||
}
|
||||
|
||||
return trim($childClasses[0]);
|
||||
@@ -621,7 +620,6 @@ class Reflection
|
||||
* @param bool $verifyParents If is set to true, parent classes are verified if they use given
|
||||
* trait. Otherwise - not.
|
||||
* @throws CannotResolveClassNameException
|
||||
* @throws ReflectionException
|
||||
* @return bool|null
|
||||
*/
|
||||
public static function usesTrait($class, $trait, $verifyParents = false)
|
||||
@@ -633,7 +631,7 @@ class Reflection
|
||||
* Oops, cannot resolve class
|
||||
*/
|
||||
if (empty($className)) {
|
||||
throw new CannotResolveClassNameException($class);
|
||||
throw CannotResolveClassNameException::create($class);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user