-
Notifications
You must be signed in to change notification settings - Fork 0
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
Approve code review #6
Changes from 1 commit
adbdeab
fc13c82
2cc0eff
a3b1487
9c41678
358db04
b445fe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,39 +8,16 @@ | |
|
||
class SpecificRateRecord extends CountableProviderRecords | ||
{ | ||
private $base; | ||
|
||
private $time; | ||
|
||
private $quote; | ||
|
||
private $rate; | ||
|
||
public function __construct(string $time, string $base, string $quote, float $rate) | ||
{ | ||
$this->base = $base; | ||
$this->time = $time; | ||
$this->quote = $quote; | ||
$this->rate = $rate; | ||
} | ||
|
||
public function getBase(): string | ||
{ | ||
return $this->base; | ||
} | ||
|
||
public function getTime(): string | ||
{ | ||
return $this->time; | ||
} | ||
|
||
public function getQuote(): string | ||
{ | ||
return $this->quote; | ||
public function __construct( | ||
\Iterator $providerRecords, | ||
int $count, | ||
ProviderResource $resource | ||
) { | ||
parent::__construct($providerRecords, $count, $resource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A constructor override that only calls the parent implementation is the same as having no constructor override. That is, this whole constructor can be deleted and have exactly the same effect. However, I also suggest (as previously), that this entire file should be deleted because custom collections are usually to supply metadata but this class is not adding any extra value, let alone supplying metadata. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I will remove this collection record. |
||
} | ||
|
||
public function getRate(): float | ||
public function toAssociativeArray(): array | ||
{ | ||
return $this->rate; | ||
return iterator_to_array($this); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,17 +38,17 @@ public function fetch(ImportConnector $connector): \Iterator | |
); | ||
|
||
$rates = $response['rates']; | ||
$base = $response['asset_id_base']; | ||
$quote = []; | ||
$rate = []; | ||
$time = []; | ||
|
||
foreach($rates as $key => $value) { | ||
$quote[] = $rates[$key]['asset_id_quote']; | ||
$rate[] = $rates[$key]['rate']; | ||
$time[] = $rates[$key]['time']; | ||
} | ||
|
||
return new AllRatesRecord($time, $base, $quote, $rate); | ||
|
||
$rate = function () use ($rates) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This closure seems to just copy the array into an iterator. You can replace this entire code block with |
||
foreach ($rates as $key => $value) { | ||
yield [ | ||
'asset_id_quote' => $rates[$key]['asset_id_quote'], | ||
'rate' => $rates[$key]['rate'], | ||
'time' => $rates[$key]['time'], | ||
]; | ||
} | ||
}; | ||
|
||
return new AllRatesRecord($rate(), $response['asset_id_base'], count($rates), $this); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,12 +39,15 @@ public function fetch(ImportConnector $connector): \Iterator | |
true | ||
); | ||
|
||
$base = $response['asset_id_base']; | ||
|
||
$quote = $response['asset_id_quote']; | ||
$rate = $response['rate']; | ||
$time = $response['time']; | ||
|
||
return new SpecificRateRecord($time, $base, $quote, $rate); | ||
$rate = function () use ($response) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this seems to be a very roundabout way of copying an array. Since we don't need the |
||
yield [ | ||
'asset_id_base' => $response['asset_id_base'], | ||
'asset_id_quote' => $response['asset_id_quote'], | ||
'rate' => $response['rate'], | ||
'time' => $response['time'], | ||
]; | ||
}; | ||
|
||
return new SpecificRateRecord($rate(), count($response), $this); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,13 @@ public function testGetSpecificRateRecords() | |
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey))) | ||
->findFirstCollection(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be necessary. |
||
|
||
$this->assertSame('BTC', $rates->getBase()); | ||
$this->assertSame('USD', $rates->getQuote()); | ||
$this->assertLessThanOrEqual(time(), strtotime($rates->getTime())); | ||
$this->assertInternalType('float', $rates->getRate()); | ||
$rateRecords = $rates->toAssociativeArray(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be necessary. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you add the wrong place for this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They're both related. This line should be deleted because it is not necessary if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give some sample code about this? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
$this->assertCount(1, $rateRecords); | ||
$this->assertArrayHasKey('asset_id_base', $rateRecords[0]); | ||
$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); | ||
$this->assertArrayHasKey('rate', $rateRecords[0]); | ||
$this->assertArrayHasKey('time', $rateRecords[0]); | ||
} | ||
|
||
public function testGetAllRateRecords() | ||
|
@@ -33,16 +36,12 @@ public function testGetAllRateRecords() | |
$rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey))) | ||
->findFirstCollection(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you show some example approach for this? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just call |
||
|
||
$base = $rates->getBase(); | ||
$quote = $rates->getQuote(); | ||
$time = $rates->getTime(); | ||
$rate = $rates->getRate(); | ||
|
||
$this->assertSame('BTC', $base); | ||
$this->assertContains('USD', $quote); | ||
$this->assertContains('GBP', $quote); | ||
$this->assertContains('EUR', $quote); | ||
$this->assertLessThanOrEqual(time(), strtotime($time[0])); | ||
$this->assertInternalType('float', $rate[0]); | ||
$this->assertSame('BTC', $rates->getBase()); | ||
|
||
$rateRecords = $rates->toAssociativeArray(); | ||
|
||
$this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); | ||
$this->assertArrayHasKey('rate', $rateRecords[0]); | ||
$this->assertArrayHasKey('time', $rateRecords[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bad pattern for testing iterators: we should ensure the whole iterator looks and works as we expect, instead of only looking at the first record. Use |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to add any value, and its implementation encourages using array instead of iterators. If the client want to use arrays it can call
iterator_to_array
itself, but Porter and her providers should not encourage this because it loads the entire data set into memory. In this case, since we're not streaming the response, it's already loaded into memory anyway, but it's still a bad idea to encourage this. Recommend removing this method.