-
-
Notifications
You must be signed in to change notification settings - Fork 179
refact!: Move field methods to Content\Field
#7082
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
base: v6/develop
Are you sure you want to change the base?
Conversation
55012fa
to
d02ec9c
Compare
Not relevant, comment was based on incorrect assumptionOne behavior change not listed in the TODO section for this PR: Currently, method access with
This means that currently it's possible to write A way to keep this backwards-compatible would be to have a mapping of public methods, then in class Field
{
private static $builtinMethods = [
'isvalid' => 'isValid',
'kirbytext' => 'kirbytext',
// …
];
public function __call(string $method, array $arguments = []): mixed
{
$method = strtolower($method);
if ($this->hasMethod($method) === true) {
return $this->callMethod($method, [clone $this, ...$arguments]);
} else if (isset(static::$builtinMethods[$method])) {
return $this->{static::$builtinMethods[$method]}(...$arguments);
} else {
// TODO: throw deprecation, then exception
// when unknown method is called
}
return $this;
}
} (Alternatively, some of that logic — or something a bit more elaborate — could move to the |
@fvsch this is not needed. PHP Methods are case insensitive by default. |
@bastianallgeier Ha! I did not know that. 😅 |
52f516b
to
500a8c8
Compare
f38c393
to
dd5605f
Compare
Content\Field
classContent\Field
dd5605f
to
f62882f
Compare
f62882f
to
6beaebf
Compare
6beaebf
to
fade3aa
Compare
Content\Field
Content\Field
$this->assertTrue(V::size($field, 3, '<')); | ||
$this->assertTrue(V::size($field, 1, '>')); | ||
$this->assertFalse(V::size($field, 1, '<')); | ||
$this->assertFalse(V::size($field, 3, '>')); |
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.
Explanation:
I corrected these, as calling the non-existent field method $field->foo()
would just return $field
anyways. V::size()
itself takes care of applying the ->value()
field method.
fade3aa
to
94a4349
Compare
'isFalse' => function (Field $field): bool { | ||
return $field->toBool() === false; | ||
}, |
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.
Comment:
Alternatively to deleting the file we could add some legacy code to ease breaking changes where users require the config file to extend certain field methods. But not sure if this really helps or just delays the pain.
'isFalse' => fn (Field $field) => $field->isFalse()
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.
When we no longer allow to overwrite field methods, extending should not be an issue, right? It feels pretty dirty to keep all the old code around for a very unlikely scenario. If we see that a lot of people need it during the alpha or beta, we could still do that.
94a4349
to
9d2a3dc
Compare
cfa9f36
to
6ae111e
Compare
9f6d030
to
a0c3a02
Compare
905f8d2
to
63e870d
Compare
Resolves #7078 BREAKING CHANGE: Removed `Kirby\Content\Field::$aliases`. BREAKING CHANGE: Removed `Kirby\Cms\Core::fieldMethods()` and `Kirby\Cms\Core::fieldMethodsAliases()`.
BREAKING CHANGE: When calling an unknown method on a field a BadMethodCallException is now thrown.
63e870d
to
477afe9
Compare
Finally coming back to this and my first thought was that we could move all the field methods into a FieldMethods trait or maybe into multiple ones by group. I think having them all in a single file is quite overwhelming when we have to deal with core field logic. |
@bastianallgeier Moved them into a |
93eca28
to
3341fde
Compare
Co-authored-by: Bastian Allgeier <mail@bastianallgeier.com>
I keep on thinking about how we could potentially have custom methods also in a trait or custom class, but I guess there really is no good solution for this. |
@bastianallgeier I think there would be a way but not sure that's actually more helpful for developers: |
There's none if we don't manage to get IDE support through that. |
try { | ||
return parent::append($args[0]->id(), $args[0]); | ||
} catch (Throwable) { | ||
// is_callable might be true when the object implements | ||
// the magic __call method, but __call can still throw | ||
// an exception | ||
} |
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.
Questions
: How is this related to the field method rewrite? Do we really need to swallow all throwables here?
If it's just about ->id()
potentially failing and there is no other fix for that, I think it would be safer to swallow throwables only for that call, not for the whole parent::append()
call.
I think everything we would do at runtime would have a really (really) hard time of being picked up by IDEs. And since plugins need to register their extensions, the only way to pick those up would be a custom Kirby extension for IDE language servers. By that point, it would not really matter how we implement it in Kirby. Runtime stays runtime. So I think there's nothing we can really do there. About overwriting core methods: I would assume that some devs have done that despite the docs. But I guess we will see during the pre-release phase if it will be a notable problem. We could still go with Florens' suggested solution for specific tags should it become relevant. |
Description
Discuss
replace
method even though this is in the core. Change?Summary of changes
config/methods.php
intoKirby\Content\Field
classKirby\Content\Field::$aliases
array and instead added aliases as actual methods that call the main methodsField::value($value)
is now bound to the cloned new field objectCore::fieldMethods()
andCore::fieldMethodsAliases()
Reasoning
Allows IDEs etc. to actually "understand" field methods, their parameter and return type hints etc.
Changelog
Refactoring
Kirby\Content\Field
Breaking changes
Kirby\Exception\BadMethodCallException
Kirby\Content\Field::$aliases
Kirby\Cms\Core::fieldMethods()
andKirby\Cms\Core::fieldMethodsAliases()
Ready?
For review team