Skip to content

Commit

Permalink
Fix issue where conical frustum is subtracted instead of added
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeboer committed Aug 16, 2019
1 parent b7f2650 commit 0713a8f
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/Common/Abstracts/AbstractSolid.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

abstract class AbstractSolid implements Solid
{
/** @var ConverterInterface */
/**
* @var ConverterInterface
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $converter;

public function setConverter (?ConverterInterface $converter): void
Expand Down
14 changes: 4 additions & 10 deletions src/Common/Abstracts/AbstractVolumetricUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

namespace Dbt\Volumes\Common\Abstracts;

use Dbt\Volumes\Common\Interfaces\LinearUnit as LinearUnitInterface;
use Dbt\Volumes\Common\Interfaces\VolumetricUnit as UnitInterface;
use Dbt\Volumes\Common\Interfaces\LinearUnit;
use Dbt\Volumes\Common\Interfaces\VolumetricUnit;

abstract class AbstractVolumetricUnit implements UnitInterface
abstract class AbstractVolumetricUnit implements VolumetricUnit
{
/** @var string */
protected $postfix = '';

/** @var string */
protected $name = '';

/** @var string */
protected $base = '';

public function postfix (): string
{
return $this->postfix;
Expand All @@ -31,8 +28,5 @@ public function __toString (): string
return $this->postfix();
}

public function getBaseLinearUnit (): LinearUnitInterface
{
return new $this->base;
}
abstract public function getBaseLinearUnit (): LinearUnit;
}
1 change: 1 addition & 0 deletions src/Common/Exceptions/NoConversionFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class NoConversionFound extends Exception
{
/** @var string */
public static $format = 'No conversion found for %s to %s';

public static function of (string $from, string $to): self
Expand Down
5 changes: 0 additions & 5 deletions src/Common/Interfaces/AngularDim.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
interface AngularDim extends Dim
{
public function unit (): AngularUnit;

/**
* @param \Dbt\Volumes\Common\Interfaces\AngularUnit|null $unit
* @return \Dbt\Volumes\Common\Interfaces\AngularDim
*/
public function of (float $value, $unit);
public function max (float $value): AngularDim;
}
7 changes: 6 additions & 1 deletion src/Common/Interfaces/Dim.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ interface Dim
{
public function value (): float;
public function hasSameUnit (Dim $dim): bool;

/**
* @param \Dbt\Volumes\Common\Interfaces\VolumetricUnit|\Dbt\Volumes\Common\Interfaces\LinearUnit|\Dbt\Volumes\Common\Interfaces\AngularUnit $unit
* @return \Dbt\Volumes\Common\Interfaces\VolumetricDim|\Dbt\Volumes\Common\Interfaces\LinearDim|\Dbt\Volumes\Common\Interfaces\AngularDim
*/
public function of (float $value, $unit);

/**
* @return \Dbt\Volumes\Common\Interfaces\VolumetricUnit|\Dbt\Volumes\Common\Interfaces\LinearUnit
* @return \Dbt\Volumes\Common\Interfaces\VolumetricUnit|\Dbt\Volumes\Common\Interfaces\LinearUnit|\Dbt\Volumes\Common\Interfaces\AngularUnit
*/
public function unit ();
}
6 changes: 1 addition & 5 deletions src/Common/Interfaces/LinearDim.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ interface LinearDim extends Dim
public function unit (): LinearUnit;
public function times (float $multiplier): LinearDim;
public function minus (LinearDim $dim): LinearDim;
public function plus (LinearDim $dim): LinearDim;
public function max (float $value): LinearDim;
public function lessThan (LinearDim $dim): bool;

/**
* @param \Dbt\Volumes\Common\Interfaces\LinearUnit|null $unit
* @return \Dbt\Volumes\Common\Interfaces\LinearDim
*/
public function of (float $value, $unit);
}
5 changes: 0 additions & 5 deletions src/Common/Interfaces/VolumetricDim.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ interface VolumetricDim extends Dim
public function unit (): VolumetricUnit;
public function times (float $multiplier): VolumetricDim;
public function plus (VolumetricDim $addend): VolumetricDim;

/**
* @param \Dbt\Volumes\Common\Interfaces\VolumetricUnit|null $unit
* @return \Dbt\Volumes\Common\Interfaces\VolumetricDim
*/
public function of (float $value, $unit);
}
5 changes: 4 additions & 1 deletion src/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
class Composite extends AbstractSolid implements Model
{
/** @var \Dbt\Volumes\Common\Interfaces\Solid[] */
private $items;
private $items = [];

/**
* @param Solid|Solid[] $solids
*/
public function __construct ($solids, Converter $converter = null)
{
$this->setConverter($converter);
Expand Down
2 changes: 1 addition & 1 deletion src/ConicalFrustumWithAngle.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function __construct (
$opposite = new Line(tan($angle->value()), $this->baseLinearUnit());

/** @var RadialDim $bottom */
$this->bottom = $this->top->minus($opposite);
$this->bottom = $this->top->plus($opposite);
}
}
4 changes: 2 additions & 2 deletions src/Converters/Conversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public static function listing (): array
'radian' => [
'degree' => function (float $value): float {
return $value * (180 / pi());
}
},
],
'degree' => [
'radian' => function (float $value): float {
return $value * (pi() / 180);
}
},
],
'none' => [
'inch' => function (float $value): float {
Expand Down
7 changes: 7 additions & 0 deletions src/Dimensions/Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function minus (LinearDim $dim): LinearDim
return $this->of($this->value() - $dim->value());
}

public function plus (LinearDim $dim): LinearDim
{
$this->assertSameUnit($dim);

return $this->of($this->value() + $dim->value());
}

/*
* Comparison
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Units/CubicInch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dbt\Volumes\Units;

use Dbt\Volumes\Common\Abstracts\AbstractVolumetricUnit;
use Dbt\Volumes\Common\Interfaces\LinearUnit;

class CubicInch extends AbstractVolumetricUnit
{
Expand All @@ -12,6 +13,8 @@ class CubicInch extends AbstractVolumetricUnit
/** @var string */
protected $name = 'cubic inch';

/** @var string */
protected $base = Inch::class;
public function getBaseLinearUnit (): LinearUnit
{
return new Inch();
}
}
7 changes: 5 additions & 2 deletions src/Units/CubicMillimeter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Dbt\Volumes\Units;

use Dbt\Volumes\Common\Abstracts\AbstractVolumetricUnit;
use Dbt\Volumes\Common\Interfaces\LinearUnit;

class CubicMillimeter extends AbstractVolumetricUnit
{
Expand All @@ -12,6 +13,8 @@ class CubicMillimeter extends AbstractVolumetricUnit
/** @var string */
protected $name = 'cubic millimeter';

/** @var string */
protected $base = Millimeter::class;
public function getBaseLinearUnit (): LinearUnit
{
return new Millimeter();
}
}
2 changes: 1 addition & 1 deletion tests/ConicalFrustumWithAngleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function calculating_the_volume ()
$height = new Line(2, $unit);
$angle = new Angle(3, $unit);
/** @var \Dbt\Volumes\Common\Interfaces\RadialDim $bottom */
$bottom = $top->minus(new Radius(tan(3), $unit));
$bottom = $top->plus(new Radius(tan(3), $unit));

$shape1 = new ConicalFrustum($top, $bottom, $height);
$shape2 = new ConicalFrustumWithAngle($top, $height, $angle);
Expand Down
3 changes: 0 additions & 3 deletions tests/Dimensions/AngleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace Dbt\Volumes\Tests\Dimensions;

use Dbt\Volumes\Dimensions\Angle;
use Dbt\Volumes\Dimensions\Line;
use Dbt\Volumes\Tests\UnitTestCase;
use Dbt\Volumes\Units\Degree;
use Dbt\Volumes\Units\Inch;
use Dbt\Volumes\Units\Millimeter;
use Dbt\Volumes\Units\None;
use Dbt\Volumes\Units\Radian;

Expand Down
27 changes: 27 additions & 0 deletions tests/Dimensions/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,31 @@ public function failing_to_compare ()

$vo1->lessThan($vo2);
}

/** @test */
public function adding_immutably ()
{
$value1 = (float) rand(1, 99999);
$value2 = (float) rand(1, 99999);
$expected = $value1 + $value2;
$unit = new None();

$vo1 = new Line($value1, $unit);
$vo2 = new Line($value2, $unit);

$this->assertSame($expected, $vo1->plus($vo2)->value());
$this->assertNotSame($vo1, $vo1->plus($vo2));
$this->assertNotSame($vo2, $vo1->plus($vo2));
}

/** @test */
public function failing_to_add ()
{
$this->expectException(WrongUnit::class);

$vo1 = new Line(1.0, new Inch());
$vo2 = new Line(2.0, new Millimeter());

$vo1->plus($vo2);
}
}
2 changes: 0 additions & 2 deletions tests/Dimensions/VolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use Dbt\Volumes\Common\Exceptions\WrongUnit;
use Dbt\Volumes\Dimensions\Volume;
use Dbt\Volumes\Tests\UnitTestCase;
use Dbt\Volumes\Units\CubicInch;
use Dbt\Volumes\Units\CubicMillimeter;
use Dbt\Volumes\Units\Inch;
use Dbt\Volumes\Units\None;

class VolumeTest extends UnitTestCase
Expand Down

0 comments on commit 0713a8f

Please sign in to comment.