Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attributes type #66

Merged
merged 3 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Attribute/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Duration implements AttributeInterface
private $milliseconds;

/**
* @param $duration
* @param string $duration
Copy link
Contributor

@greg0ire greg0ire Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|int, because the class should not know what it will be called with. In php7, you will use int as type hinting (which accepts numeric strings IIRC)

Copy link
Contributor

@greg0ire greg0ire Jan 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, PHP will coerce values of the wrong type into the expected scalar type if possible. For example, a function that is given an integer for a parameter that expects a string will get a variable of type string.

So when you drop php 5 (on next major release?), you can safely add int as type hinting indeed.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

*/
public function __construct($duration)
{
$this->milliseconds = $duration;
$this->milliseconds = (int) $duration;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Attribute/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Rate implements AttributeInterface
use DumpTrait;

/**
* @var int
* @var float
*/
private $absoluteValue;

Expand All @@ -19,17 +19,17 @@ class Rate implements AttributeInterface
private $textValue;

/**
* @param $absoluteValue
* @param $textValue
* @param string $absoluteValue
* @param string $textValue
*/
public function __construct($absoluteValue, $textValue)
{
$this->absoluteValue = $absoluteValue;
$this->absoluteValue = (float) $absoluteValue;
$this->textValue = $textValue;
}

/**
* @return int
* @return float
*/
public function getAbsoluteValue()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Size implements AttributeInterface
private $bit;

/**
* @param int $size
* @param string $size
*/
public function __construct($size)
{
$this->bit = $size;
$this->bit = (int) $size;
}

/**
Expand Down
15 changes: 9 additions & 6 deletions test/Attribute/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public function testCover()

public function testDuration()
{
$duration = new Duration(1000);
$this->assertEquals(1000, $duration->getMilliseconds());
$duration = new Duration('1000');
$this->assertSame(1000, $duration->getMilliseconds());
$this->assertTrue(is_int($duration->getMilliseconds()));
$this->assertSame('1000', (string) $duration);
}

Expand All @@ -34,16 +35,18 @@ public function testMode()

public function testRate()
{
$rate = new Rate(15555, '15.55 Mo');
$this->assertEquals(15555, $rate->getAbsoluteValue());
$rate = new Rate('15555', '15.55 Mo');
$this->assertSame(15555.0, $rate->getAbsoluteValue());
$this->assertTrue(is_float($rate->getAbsoluteValue()));
$this->assertEquals('15.55 Mo', $rate->getTextValue());
$this->assertSame('15.55 Mo', (string) $rate);
}

public function testSize()
{
$size = new Size(42);
$this->assertEquals(42, $size->getBit());
$size = new Size('42');
$this->assertSame(42, $size->getBit());
$this->assertTrue(is_int($size->getBit()));
$this->assertSame('42', (string) $size);
}
}