-
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
Conversation
891c458
to
925196d
Compare
tests/ContainsManyTest.php
Outdated
@@ -19,8 +19,9 @@ | |||
/** | |||
* Invoice model. | |||
* | |||
* @property Line2 $lines @Atk\RefMany() | |||
* @property string $total_gross @Atk\Field() | |||
* @property Line2 $lines @Atk\RefOne() |
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.
always use RefOne
, RefMany
should be used once load()
returns another instance (otherwise you will not be able to laod a model, you must get it thru iterator first)
tests/ContainsManyTest.php
Outdated
|
||
// total_gross - calculated by php callback not by SQL expression | ||
$this->addCalculatedField($this->fieldName()->total_gross, function (self $m) { | ||
$total = 0; | ||
foreach ($m->ref('lines') as $line) { | ||
$total += $line->get('total_gross'); | ||
foreach ($m->lines as $line) { |
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 is one strong advantage of hintable magic, thanks to #821 foreach value has correct type for phpstan as the iterated model
a73db73
to
c87335f
Compare
c87335f
to
f184be7
Compare
@@ -80,7 +80,7 @@ public function addRef(string $link, array $defaults): Reference | |||
* | |||
* @return Reference\HasOne | |||
*/ | |||
public function hasOne(string $link, array $defaults = []): Reference | |||
public function hasOne(string $link, array $defaults = []) //: Reference |
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?
public function hasOne(string $link, array $defaults = []): 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.
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.
26edb56
to
781497d
Compare
781497d
to
17988b4
Compare
0032e48
to
5f9863c
Compare
5f9863c
to
36e90f5
Compare
related with #800