Skip to content

Commit

Permalink
Merge 8a566f2 into d130585
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Sep 27, 2015
2 parents d130585 + 8a566f2 commit bef9a7b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 131 deletions.
151 changes: 49 additions & 102 deletions src/Util.php
Expand Up @@ -64,32 +64,14 @@ public static function getExceptionInfo(\Exception $e)
*/
public static function ensure($valueToEnsure, $valueToCheck, $exception = null, array $exceptionArgs = null)
{
if ($valueToEnsure === $valueToCheck) {
return $valueToCheck;
if ($valueToEnsure !== $valueToCheck) {
throw self::buildException(
$exception ?: "'{$valueToEnsure}' did not equal '{$valueToCheck}'",
$exceptionArgs
);
}

if ($exception === null) {
throw new \Exception("'{$valueToEnsure}' did not equal '{$valueToCheck}'");
}

if (is_string($exception)) {
if ($exceptionArgs === null) {
throw new \Exception($exception);
}

if (array_key_exists($exception, self::$exceptionAliases)) {
$exception = self::$exceptionAliases[$exception];
}

$reflectionClass = new \ReflectionClass($exception);
throw $reflectionClass->newInstanceArgs($exceptionArgs);
}

if ($exception instanceof \Exception) {
throw $exception;
}

throw new \InvalidArgumentException('$exception was not null, a string, or an Exception');
return $valueToCheck;
}

/**
Expand All @@ -115,32 +97,44 @@ public static function ensure($valueToEnsure, $valueToCheck, $exception = null,
*/
public static function ensureNot($valueToThrowOn, $valueToCheck, $exception = null, array $exceptionArgs = null)
{
if ($valueToThrowOn !== $valueToCheck) {
return $valueToCheck;
if ($valueToThrowOn === $valueToCheck) {
throw self::buildException($exception ?: "'{$valueToThrowOn}' equals '{$valueToCheck}'", $exceptionArgs);
}

if ($exception === null) {
throw new \Exception("'{$valueToThrowOn}' equals '{$valueToCheck}'");
}
return $valueToCheck;
}

if (is_string($exception)) {
if ($exceptionArgs === null) {
throw new \Exception($exception);
}
/**
* Helper method to return exception created from ensure[Not] call inpu.
*
* @param mixed $exception Null, a fully qualified exception class name, string for an Exception message,
* or an Exception. The fully qualified exception class name could also be an
* alias in getExceptionAliases()
* @param array|null $exceptionArgs Arguments to pass to a new instance of $exception. If using this parameter make
* sure these arguments match the constructor for an exception of type $exception.
*
* @throws \InvalidArgumentException if $exception was not null, a string, or an Exception
*/
private static function buildException($exception, array $exceptionArgs = null)
{
if ($exception instanceof \Exception) {
return $exception;
}

if (array_key_exists($exception, self::$exceptionAliases)) {
$exception = self::$exceptionAliases[$exception];
}
if (!is_string($exception)) {
throw new \InvalidArgumentException('$exception was not null, a string, or an Exception');
}

$reflectionClass = new \ReflectionClass($exception);
throw $reflectionClass->newInstanceArgs($exceptionArgs);
if ($exceptionArgs === null) {
return new \Exception($exception);
}

if ($exception instanceof \Exception) {
throw $exception;
if (array_key_exists($exception, self::$exceptionAliases)) {
$exception = self::$exceptionAliases[$exception];
}

throw new \InvalidArgumentException('$exception was not null, a string, or an Exception');
$reflectionClass = new \ReflectionClass($exception);
return $reflectionClass->newInstanceArgs($exceptionArgs);
}

/**
Expand Down Expand Up @@ -194,70 +188,23 @@ public static function throwIfNotType(array $typesToVariables, $failOnWhitespace
$variables = $variablesOrVariable;
}

//cast ok since an integer won't match any of the cases.
//the similar code in the cases is an optimization for those type where faster checks can be made.
switch ((string)$type) {
case 'bool':
foreach ($variables as $i => $variable) {
//using the continue here not negative checks to make use of short cutting optimization.
if ($variable === false || $variable === true || ($allowNulls && $variable === null)) {
continue;
}

throw new \InvalidArgumentException("variable at position '{$i}' was not a boolean");
}

break;
case 'null':
foreach ($variables as $i => $variable) {
if ($variable !== null) {
throw new \InvalidArgumentException("variable at position '{$i}' was not null");
}
}

break;
case 'string':
foreach ($variables as $i => $variable) {
if (is_string($variable)) {
if ($failOnWhitespace && trim($variable) === '') {
throw new \InvalidArgumentException("variable at position '{$i}' was whitespace");
}

continue;
}

if ($allowNulls && $variable === null) {
continue;
}

throw new \InvalidArgumentException("variable at position '{$i}' was not a '{$type}'");
}
$isFunction = "is_{$type}";
if (!function_exists($isFunction)) {
throw new \InvalidArgumentException('a type was not one of the is_ functions');
}

break;
case 'array':
case 'callable':
case 'double':
case 'float':
case 'int':
case 'integer':
case 'long':
case 'numeric':
case 'object':
case 'real':
case 'resource':
case 'scalar':
$isFunction = "is_{$type}";
foreach ($variables as $i => $variable) {
if ($isFunction($variable) || ($allowNulls && $variable === null)) {
continue;
}
foreach ($variables as $i => $variable) {
if ($variable === null && $allowNulls) {
continue;
}

throw new \InvalidArgumentException("variable at position '{$i}' was not a '{$type}'");
}
if (!$isFunction($variable)) {
throw new \InvalidArgumentException("variable at position '{$i}' was not a '{$type}'");
}

break;
default:
throw new \InvalidArgumentException('a type was not one of the is_ functions');
if ($type === 'string' && $failOnWhitespace && trim($variable) === '') {
throw new \InvalidArgumentException("variable at position '{$i}' was whitespace");
}
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/Util/Arrays.php
Expand Up @@ -219,19 +219,20 @@ public static function embedInto(array $items, $fieldName, array $destination =
}

foreach ($items as $key => $item) {
if (array_key_exists($key, $destination)) {
if (!is_array($destination[$key])) {
throw new \InvalidArgumentException('a value in $destination was not an array');
}
if (!array_key_exists($key, $destination)) {
$destination[$key] = [$fieldName => $item];
continue;
}

if (!$overwrite && array_key_exists($fieldName, $destination[$key])) {
throw new \Exception('$fieldName key already exists in a $destination array');
}
if (!is_array($destination[$key])) {
throw new \InvalidArgumentException('a value in $destination was not an array');
}

$destination[$key][$fieldName] = $item;
} else {
$destination[$key] = [$fieldName => $item];
if (!$overwrite && array_key_exists($fieldName, $destination[$key])) {
throw new \Exception('$fieldName key already exists in a $destination array');
}

$destination[$key][$fieldName] = $item;
}

return $destination;
Expand Down
25 changes: 8 additions & 17 deletions src/Util/File.php
Expand Up @@ -30,27 +30,18 @@ public static function deleteDirectoryContents($directoryPath)
throw new \InvalidArgumentException('$directoryPath is not a string');
}

$paths = scandir($directoryPath);
if ($paths === false) {
throw new \Exception("cannot list directory '{$directoryPath}'");
}
foreach (new \FileSystemIterator($directoryPath) as $path => $fileInfo) {
if ($fileInfo->isDir()) {
self::deleteDirectoryContents($path);//RECURSIVE CALL
if (!rmdir($path)) {
throw new \Exception("cannot delete '{$fullPath}'", 1);
}

foreach ($paths as $path) {
if ($path === '.' || $path === '..') {
continue;
}

$fullPath = "{$directoryPath}/{$path}";

if (is_dir($fullPath)) {
self::deleteDirectoryContents($fullPath);//RECURSIVE CALL
if (!rmdir($fullPath)) {
throw new \Exception("cannot delete '{$fullPath}'", 1);
}
} else {
if (!unlink($fullPath)) {
throw new \Exception("cannot delete '{$fullPath}'", 2);
}
if (!unlink($path)) {
throw new \Exception("cannot delete '{$path}'", 2);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Util/FileTest.php
Expand Up @@ -77,8 +77,7 @@ public function deleteDirectoryContentsNonStringPath()
/**
* @test
* @covers ::deleteDirectoryContents
* @expectedException \Exception
* @expectedExceptionMessage cannot list directory '/some/where/that/doesnt/exist'
* @expectedException \UnexpectedValueException
*/
public function deleteDirectoryContentsNonExistentPath()
{
Expand Down
1 change: 1 addition & 0 deletions tests/UtilTest.php
Expand Up @@ -9,6 +9,7 @@

/**
* @coversDefaultClass \DominionEnterprises\Util
* @covers ::<private>
*/
final class UtilTest extends \PHPUnit_Framework_TestCase
{
Expand Down

0 comments on commit bef9a7b

Please sign in to comment.