+
Skip to content

Conversation

amulet1
Copy link
Collaborator

@amulet1 amulet1 commented Jun 13, 2025

As far as I understand, by default (without &) PHP passes non-objects (which includes arrays) by value.

For example:

<?php
function test($info, $message) {
        $info['test'] = 123;
        $message = 'test';
}

$info = [ 'test' => 222 ];
$message = 'message';
print "test=" . $info['test'] . " message=$message\n";

test($info, $message);

print "test=" . $info['test'] . " message=$message\n";

Output (nothing changes):

test=222 message=message
test=222 message=message

The commit 070af29 removed & from $message in IsValid() signature. This causes loss of value possibly assigned to $message.

Possible example of other signature issue (not in this patch, but I can update it, if needed) is in getInfo. Implementations are inconsistent - some return $info, others modify its in place.

But in the below case nothing is returned and $info is not passed by reference, so the assigned value is lost:

class Horde_Form_Type_stringarray extends Horde_Form_Type_stringlist {
    function getInfo($vars, $var, $info)
    {
        $info = array_map('trim', explode(',', $vars->get($var->getVarName())));
    }

If this PR is accepted, signatures in other apps (for example, see Ingo_Form_Type_Longemail::isValid()) that extend Horde_Form_Type or its derivatives need to be updated as well.

The $message parameter is string and it should be passed by reference.

See commit 070af2
@ralflang
Copy link
Member

I'm not so fond of this approach. Alternatives:

  • Store a property $invalidMessage either with a read hook (PHP 8.4+ only) or with a getter (universally possible)
  • Emit a rich object if valid.

As for the getInfo, we have made virtually all getInfo usages return the value and read the return value. Let me look up any leftovers.

@amulet1
Copy link
Collaborator Author

amulet1 commented Jun 19, 2025

In this case $message parameter should be dropped altogether.

And all the callers should be updated to retrieve the property, if it is needed.

getInfo still needs to be fixed ($infoparameter should be dropped too).

I would like to offer my help with the changes. Please advise.

@ralflang
Copy link
Member

Help is much appreciated. However we should be careful with timing.

Changing signatures again in a base class which is littered all over horde and external products needs a bit of care. We want to go beta next week and stable in July. Let's work on Forms 4 with a proper OO model and then upgrade apps step by step in a .1 feature release.

@amulet1
Copy link
Collaborator Author

amulet1 commented Jun 19, 2025

Well, right now it is broken - $message is not returned at all. It has to be fixed.

Maybe we can do it in stages:

  1. Make $message parameter optional. Potentially log warning/stack trace if the argument count includes the optional parameter.
  2. Eliminate it on the caller's end, update code to retrieve $message property where needed. This can take some time.
  3. Finally remove $message parameter.

Same can be done with getInfo. Not sure if there are other places like that, as I just looked at the very recent commits that were not working for me.

@ralflang
Copy link
Member

ralflang commented Jun 20, 2025

Yes, let's do it in stages.

  • Callers may upgrade right now:
// old:
$form->getInfo($vars, $info)
$type->getInfo($vars, $info);
$bValid = $type->isValid($var, $vars, $value, $message);

// new:
$info = $form->getInfo($vars);
$info = $type->getInfo($vars, $info);
$bValid = $type->isValid($var, $vars, $value, $message);
$message = $type->getMessage();

Let's implement the incompatible, better type system in the PSR-4 namespace (src folder)

src/V3/Types/Text.php

$info = $form->getInfo($vars);
$info = $type->getInfo($vars);
$bValid = $type->isValid($var, $vars, $value);
$message = $type->getMessage();

This way

  • caller code than upgrade to TypesV3 at their own pace
  • There is no hard break. Most code I tested is already upgraded for the crucial getInfo functionality. The $message functionality can be updated one library/app at a time either to just add the getMessage line (right now) or to use the new signature (as soon as we have that)
  • All progressively possible before July i.e. before the Horde 6 mainline release or after. No time pressure.
  • A more involved V4 redesign can coexist.

@ralflang
Copy link
Member

Superseded by #6 - Let's do it in stages.

@ralflang ralflang closed this Jun 20, 2025
ralflang added a commit that referenced this pull request Jun 20, 2025
fix: Discussion from #5 - use a getter and property instead of a return parameter
ralflang added a commit that referenced this pull request Jun 22, 2025
fix: Add missing message assignment
chore: Add more V3 types
chore: Draft V3 design
docs(README): Add upgrading instructions
fix: Discussion from #5 - use a getter and property instead of a return parameter
@amulet1 amulet1 deleted the amulet1-patch-1 branch June 25, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载