-
Notifications
You must be signed in to change notification settings - Fork 47
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
Convert hardcoded strings to hintable fields in Contains{One, Many}Test #822
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Tests\ContainsMany; | ||
|
||
use Atk4\Data\Model; | ||
|
||
/** | ||
* Each line can have multiple discounts. | ||
* | ||
* @property int $percent @Atk\Field() | ||
* @property \DateTime $valid_till @Atk\Field() | ||
*/ | ||
class Discount extends Model | ||
{ | ||
protected function init(): void | ||
{ | ||
parent::init(); | ||
|
||
$this->addField($this->fieldName()->percent, ['type' => 'integer', 'required' => true]); | ||
$this->addField($this->fieldName()->valid_till, ['type' => 'datetime']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Tests\ContainsMany; | ||
|
||
use Atk4\Data\Model; | ||
|
||
/** | ||
* Invoice model. | ||
* | ||
* @property string $ref_no @Atk\Field() | ||
* @property float $amount @Atk\Field() | ||
* @property Line $lines @Atk\RefOne() | ||
* @property string $total_gross @Atk\Field() | ||
* @property float $discounts_total_sum @Atk\Field() | ||
*/ | ||
class Invoice extends Model | ||
{ | ||
public $table = 'invoice'; | ||
|
||
protected function init(): void | ||
{ | ||
parent:: init(); | ||
|
||
$this->title_field = $this->fieldName()->ref_no; | ||
|
||
$this->addField($this->fieldName()->ref_no, ['required' => true]); | ||
$this->addField($this->fieldName()->amount, ['type' => 'money']); | ||
|
||
// will contain many Lines | ||
$this->containsMany($this->fieldName()->lines, ['model' => [Line::class], 'caption' => 'My Invoice Lines']); | ||
|
||
// total_gross - calculated by php callback not by SQL expression | ||
$this->addCalculatedField($this->fieldName()->total_gross, function (self $m) { | ||
$total = 0; | ||
foreach ($m->lines as $line) { | ||
$total += $line->total_gross; | ||
} | ||
|
||
return $total; | ||
}); | ||
|
||
// discounts_total_sum - calculated by php callback not by SQL expression | ||
$this->addCalculatedField($this->fieldName()->discounts_total_sum, function (self $m) { | ||
$total = 0; | ||
foreach ($m->lines as $line) { | ||
$total += $line->total_gross * $line->discounts_percent / 100; | ||
} | ||
|
||
return $total; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Tests\ContainsMany; | ||
|
||
use Atk4\Data\Model; | ||
|
||
/** | ||
* Invoice lines model. | ||
* | ||
* @property VatRate $vat_rate_id @Atk\RefOne() | ||
* @property float $price @Atk\Field() | ||
* @property float $qty @Atk\Field() | ||
* @property \DateTime $add_date @Atk\Field() | ||
* @property string $total_gross @Atk\Field() | ||
* @property Discount $discounts @Atk\RefOne() | ||
* @property float $discounts_percent @Atk\Field() | ||
*/ | ||
class Line extends Model | ||
{ | ||
protected function init(): void | ||
{ | ||
parent::init(); | ||
|
||
$this->hasOne($this->fieldName()->vat_rate_id, ['model' => [VatRate::class]]); | ||
|
||
$this->addField($this->fieldName()->price, ['type' => 'money', 'required' => true]); | ||
$this->addField($this->fieldName()->qty, ['type' => 'float', 'required' => true]); | ||
$this->addField($this->fieldName()->add_date, ['type' => 'datetime']); | ||
|
||
$this->addExpression($this->fieldName()->total_gross, function (self $m) { | ||
return $m->price * $m->qty * (1 + $m->vat_rate_id->rate / 100); | ||
}); | ||
|
||
// each line can have multiple discounts and calculate total of these discounts | ||
$this->containsMany($this->fieldName()->discounts, ['model' => [Discount::class]]); | ||
|
||
$this->addCalculatedField($this->fieldName()->discounts_percent, function ($m) { | ||
$total = 0; | ||
foreach ($m->discounts as $d) { | ||
$total += $d->percent; | ||
} | ||
|
||
return $total; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Tests\ContainsMany; | ||
|
||
use Atk4\Data\Model; | ||
|
||
/** | ||
* VAT rate model. | ||
* | ||
* @property string $name @Atk\Field() | ||
* @property int $rate @Atk\Field() | ||
*/ | ||
class VatRate extends Model | ||
{ | ||
public $table = 'vat_rate'; | ||
|
||
protected function init(): void | ||
{ | ||
parent::init(); | ||
|
||
$this->addField($this->fieldName()->name); | ||
$this->addField($this->fieldName()->rate, ['type' => 'integer']); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
otherwise autohinting (from phpdoc) does not work
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.
Why not add HasOne to Method Signature?
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.
because it can be implemented by another class not based on this one
this is almost the no1 issue with Atk
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.
so you mean a child class could e.g. implement
public function hasOne(string $link, array $defaults = []): Some\Other\Reference
and that would fail if we set the parent signature to Reference\HasOne?
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.
yes
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.
grml this is ugly. Do you really think adding
Reference\HasOne
would be an issue? I see return types in method signatures as an important part of good code. While this PR helps IDEs and phpstan and so on using comments, it weakens the method signatures which was something I liked about almost any of your PR: You created stricter, clearer method signatures.