Skip to content

Commit

Permalink
code tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
chippyash committed Jun 1, 2015
1 parent 31bfdca commit cac88d4
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 60 deletions.
10 changes: 5 additions & 5 deletions src/chippyash/Type/AbstractMultiValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ abstract class AbstractMultiValueType extends AbstractType
* Map of value names to $value array position
* [pos=>[name, class], ...]
* You need to override this in child classes and set it
*
*
* `pos` refers to the order in which it is expected to be placed in the
* parameter list in constructor and set methods
*
*
* name is the name of the value
*
*
* class is the full class name of the expected value object
*
*
* @var array
*/
protected $valueMap = array();
Expand Down Expand Up @@ -154,7 +154,7 @@ protected function setFromTypes(array $params)
throw new \InvalidArgumentException("Expected {$e} parameters, got {$n}");
}

foreach ($params as $key=>$value) {
foreach ($params as $key => $value) {
if (is_object($value)) {
if (!$value instanceof $this->valueMap[$key]['class']) {
$c = get_class($value);
Expand Down
4 changes: 2 additions & 2 deletions src/chippyash/Type/Exceptions/GmpNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* Thrown if gmp support not available
*/
class GmpNotSupportedException extends \Exception {
class GmpNotSupportedException extends \Exception
{

protected $msg = 'GMP library not found';

Expand All @@ -23,5 +24,4 @@ public function __construct($ignored = null, $code = null, $previous = null)
{
parent::__construct($this->msg, $code, $previous);
}

}
4 changes: 2 additions & 2 deletions src/chippyash/Type/Exceptions/InvalidTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* Invalid type exception
*/
class InvalidTypeException extends \Exception {
class InvalidTypeException extends \Exception
{

protected $msg = 'Invalid Type: %s';

Expand All @@ -26,5 +27,4 @@ public function __construct($type, $code = null, $previous = null)
{
parent::__construct(sprintf($this->msg, $type), $code, $previous);
}

}
4 changes: 2 additions & 2 deletions src/chippyash/Type/Exceptions/NotRealComplexException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* Thrown when trying to cast complex to real
*/
class NotRealComplexException extends \Exception {
class NotRealComplexException extends \Exception
{

protected $msg = 'Not a Real complex type';

Expand All @@ -23,5 +24,4 @@ public function __construct($ignored = null, $code = null, $previous = null)
{
parent::__construct($this->msg, $code, $previous);
}

}
3 changes: 1 addition & 2 deletions src/chippyash/Type/Interfaces/GMPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function asGMPIntType();

/**
* Return the number as a GMPComplex number i.e. n+0i
*
*
* @return \chippyash\Type\Number\Complex\GMPComplexType
*/
public function asGMPComplex();
Expand All @@ -44,5 +44,4 @@ public function asGMPComplex();
* @return \chippyash\Type\Number\Rational\GMPRationalType
*/
public function asGMPRational();

}
3 changes: 2 additions & 1 deletion src/chippyash/Type/Interfaces/NumericTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* A interface to mark numeric types
*/
interface NumericTypeInterface {
interface NumericTypeInterface
{

/**
* Negates the number
Expand Down
1 change: 0 additions & 1 deletion src/chippyash/Type/Interfaces/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ public function __invoke();
* @return string
*/
public function __toString();

}
4 changes: 1 addition & 3 deletions src/chippyash/Type/Number/Complex/AbstractComplexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
/**
* Abstract complex number type
*/
abstract class AbstractComplexType extends AbstractMultiValueType
implements ComplexTypeInterface, NumericTypeInterface
abstract class AbstractComplexType extends AbstractMultiValueType implements ComplexTypeInterface, NumericTypeInterface
{
/**
* Map of values for this type
Expand Down Expand Up @@ -332,5 +331,4 @@ protected function getAsNativeType()
//return as string
return (string) $this;
}

}
1 change: 0 additions & 1 deletion src/chippyash/Type/Number/Complex/ComplexTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,4 @@ private static function getImaginaryPartsFromRadiusAndSin(RationalType $radius,
TypeFactory::create('int', $radius->denominator()->get() * $sin->denominator()->get())
) ;
}

}
6 changes: 5 additions & 1 deletion src/chippyash/Type/Number/Complex/GMPComplexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,13 @@ public function asFloatType()
*/
private function lcm($a, $b)
{
return gmp_abs(gmp_div_q(gmp_mul($a, $b),gmp_gcd($a, $b)));
return gmp_abs(gmp_div_q(gmp_mul($a, $b), gmp_gcd($a, $b)));
}

/**
* @param $value
* @return int|float
*/
private function checkIntType($value)
{
$test = intval($value);
Expand Down
24 changes: 17 additions & 7 deletions src/chippyash/Type/Number/GMPIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GMPIntType extends IntType implements GMPInterface

/**
* Constructor - check for gmp support
*
*
* @param mixed $value
* @throws GmpNotSupportedException
*/
Expand Down Expand Up @@ -58,8 +58,8 @@ public function negate()
public function asComplex()
{
return new GMPComplexType(
new GMPRationalType(clone $this, new self(1)),
new GMPRationalType(new self(0), new self(1))
new GMPRationalType(clone $this, new self(1)),
new GMPRationalType(new self(0), new self(1))
);
}

Expand Down Expand Up @@ -224,9 +224,9 @@ public function asIntType()
public function asGMPComplex()
{
return new GMPComplexType(
new GMPRationalType(new GMPIntType($this->get()), new GMPIntType(1)),
new GMPRationalType(new GMPIntType(0), new GMPIntType(1))
);
new GMPRationalType(new GMPIntType($this->get()), new GMPIntType(1)),
new GMPRationalType(new GMPIntType(0), new GMPIntType(1))
);
}

/**
Expand All @@ -239,7 +239,14 @@ public function asGMPRational()
{
return new GMPRationalType(new GMPIntType($this->get()), new GMPIntType(1));
}


/**
* Return correctly typed value for this type
*
* @param mixed $value
*
* @return GMP|\resource
*/
protected function typeOf($value)
{
if ($this->gmpTypeCheck($value)) {
Expand All @@ -249,6 +256,9 @@ protected function typeOf($value)
}
}

/**
* @return GMP|\resource
*/
protected function cloneValue()
{
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
Expand Down
1 change: 0 additions & 1 deletion src/chippyash/Type/Number/IntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,4 @@ protected function typeOf($value)
{
return intval($value);
}

}
2 changes: 1 addition & 1 deletion src/chippyash/Type/Number/NaturalIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NaturalIntType extends IntType

/**
* Negates the number
*
*
* @throws \BadMethodCallException
*/
public function negate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class AbstractRationalType extends AbstractMultiValueType implements Ra

/**
* Return the number as a Complex number i.e. n+0i
*
*
* @return \chippyash\Type\Number\Complex\ComplexType
*/
public function asComplex()
Expand Down
2 changes: 0 additions & 2 deletions src/chippyash/Type/Number/Rational/RationalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use chippyash\Type\Number\IntType;
use chippyash\Type\BoolType;


/**
* A rational number (i.e a fraction)
*
Expand Down Expand Up @@ -75,5 +74,4 @@ private function gcd($a, $b)
{
return $b ? $this->gcd($b, $a % $b) : $a;
}

}
103 changes: 78 additions & 25 deletions src/chippyash/Type/Number/Rational/RationalTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,15 @@ public static function create($numerator, $denominator = null)
return self::fromFloat($numerator);
}

if (is_numeric($numerator) && is_null($denominator)) {
return self::createCorrectRational($numerator, 1);
if (is_numeric($numerator)) {
return self::createFromNumericNumerator($numerator, $denominator);
}

if (is_numeric($numerator) && is_numeric($denominator)) {
return self::createCorrectRational($numerator, $denominator);
}

if (is_numeric($numerator) && $denominator instanceof IntType) {
return self::createCorrectRational($numerator, $denominator());
if ($numerator instanceof IntType) {
return self::createFromIntTypeNumerator($numerator, $denominator);
}

if ($numerator instanceof IntType && $denominator instanceof IntType) {
return self::createCorrectRational($numerator(), $denominator());
}

if ($numerator instanceof IntType && is_null($denominator)) {
return self::createCorrectRational($numerator(), 1);
}

if ($numerator instanceof IntType && is_numeric($denominator)) {
return self::createCorrectRational($numerator(), $denominator);
}

$typeN = gettype($numerator);
$typeD = gettype($denominator);
throw new InvalidTypeException("{$typeN}:{$typeD} for Rational type construction");
self::throwCreateException($numerator, $denominator);
}

/**
Expand Down Expand Up @@ -180,12 +162,14 @@ public static function fromString($string)
{
$matches = array();
$valid = \preg_match(
'#^(-)? *?(\d+) *?/ *?(-)? *?(\d+)$#', \trim($string), $matches
'#^(-)? *?(\d+) *?/ *?(-)? *?(\d+)$#',
\trim($string),
$matches
);

if ($valid !== 1) {
throw new \InvalidArgumentException(
'The string representation of the rational is invalid.'
'The string representation of the rational is invalid.'
);
}

Expand Down Expand Up @@ -276,4 +260,73 @@ protected static function createCorrectRational($num, $den)
return new RationalType(new IntType($num), new IntType($den));
}
}

/**
* Create where numerator is known to be numeric
*
* @param mixed $numerator Conforms to is_numeric()
* @param mixed $denominator
*
* @return GMPRationalType|RationalType
*
* @throws InvalidTypeException
*/
private static function createFromNumericNumerator($numerator, $denominator)
{
if (is_null($denominator)) {
return self::createCorrectRational($numerator, 1);
}

if (is_numeric($denominator)) {
return self::createCorrectRational($numerator, $denominator);
}

if ($denominator instanceof IntType) {
return self::createCorrectRational($numerator, $denominator());
}

self::throwCreateException($numerator, $denominator);
}

/**
* Create where numerator is known to be IntType
*
* @param IntType $numerator
* @param mixed $denominator
*
* @return GMPRationalType|RationalType
*
* @throws InvalidTypeException
*/
private static function createFromIntTypeNumerator(IntType $numerator, $denominator)
{
if ($denominator instanceof IntType) {
return self::createCorrectRational($numerator(), $denominator());
}

if (is_null($denominator)) {
return self::createCorrectRational($numerator(), 1);
}

if (is_numeric($denominator)) {
return self::createCorrectRational($numerator(), $denominator);
}

self::throwCreateException($numerator, $denominator);
}

/**
* Throw a create exception
*
* @param $numerator
* @param $denominator
*
* @throws InvalidTypeException
*/
private static function throwCreateException($numerator, $denominator)
{
$typeN = gettype($numerator);
$typeD = gettype($denominator);
throw new InvalidTypeException("{$typeN}:{$typeD} for Rational type construction");
}
}
2 changes: 1 addition & 1 deletion src/chippyash/Type/Number/WholeIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WholeIntType extends IntType

/**
* Negates the number
*
*
* @throws \BadMethodCallException
*/
public function negate()
Expand Down
8 changes: 7 additions & 1 deletion src/chippyash/Type/String/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
*/
class StringType extends AbstractType
{

/**
* Return correctly typed value for this type
*
* @param mixed $value
*
* @return string
*/
protected function typeOf($value)
{
return (string) $value;
Expand Down

0 comments on commit cac88d4

Please sign in to comment.