Skip to content

[ticket/15190] Consolidate validation methods in metadata manager#4807

Open
javiexin wants to merge 7 commits intophpbb:masterfrom
javiexin:ticket/15190
Open

[ticket/15190] Consolidate validation methods in metadata manager#4807
javiexin wants to merge 7 commits intophpbb:masterfrom
javiexin:ticket/15190

Conversation

@javiexin
Copy link
Contributor

@javiexin javiexin commented Apr 23, 2017

Use a single method to access all validation options in metadata manager. Before, some validations had their own function, while others shared a single validate method. Now, the validate method acts as a proxy for all others.
Also, with the existing implementation, you should not call validate without calling get_metadata first, as get_metadata was the only way to do the file loading (any call to validate without calling get_metadata first would result in an exception being raised even if the metadata is correct); with this change, the order is irrelevant, and the file is only loaded once. Some uses of get_metadata that were not using the returned data have been replaced with the corresponding validate call.

Checklist:

  • Correct branch: master for new features; 3.2.x, 3.1.x for fixes
  • Tests pass
  • Code follows coding guidelines: master / 3.2.x, 3.1.x
  • Commit follows commit message format

Tracker ticket:

https://tracker.phpbb.com/browse/PHPBB3-15190

@Nicofuma
Copy link
Member

All validate_* methods should be marked @internal or @deprecated with a note saying that in 4.0 they won't be public anymore.

About the validate() method, I don't really like how it works now. I think that all should validate everything.

Maybe we can work 2 arrays:

  • An array of validators (Should be a callable, [$this, 'validate_enable'] per example)
  • A map associating a target to a list of validators
    Then when validate($a) is called, we check if there is a such target or if there is a such validators and then we call them.

e.g:

$validators = [
    'enable' => [$this, 'validate_enable'],
    'dir' => [$this, 'validate_dir'],
];

$map = [
    'all' => ['enable', 'dir'],
    'display' => ['enable'],
];

function validate($target)
{
    $v = isset($map[$target]) ? $map[$target] : (array) $target;

    foreach ($v as $validator) {
        $validators[$validator](); // Plus handle faillure or anything else if necessary
    }
}

@javiexin
Copy link
Contributor Author

javiexin commented Apr 24, 2017

I like your proposal. If we are refactoring validate completely, maybe worth adding a validation (and get) for version_check, quite used in the code.
Will work on this.

While we are at this, what do you think about making metadata completely "opaque", that is, encapsulated by extension manager, using it as the only accessor to metadata manager, providing proxy methods in extension manager to validate and get metadata? That would be a separate (but related) PR. Same thing could be done for extension objects themselves (only is_enableable is accessed from outside of the direct control of extension manager).

@javiexin
Copy link
Contributor Author

javiexin commented Apr 24, 2017

Ok, I have the code, as follows:

  1. Create a single array, as a class property, filled in the constructor (extending the existing one, but moving it to a class property and initializing once)
  2. Array key is the validation name, array value is mixed, as follows:
    • null for callable methods, of the form 'validate_'$key
    • string for reg exp validation of regular fields
    • array for combined validation by nesting (self calling)
  3. The validate function accesses the array, and determines how to proceed:
    • calling the specific function
    • performing the regular expression check
    • self-calling with the component validations
      Is this the right approach?

I could revert back to three arrays, but would be as complex, and more verbose... Your choice :)

As for the other comment, I will mark all validate_ methods as internal (not deprecated) as they will continue existing, although moving them to protected visibility (as of 4.0, right?).

For now, I have not changed the semantics of any validation (all is display + enable, so really, all things that are validated are included here).

@Nicofuma
Copy link
Member

Nicofuma commented Apr 24, 2017

Lets see how it looks.

About merging the extension manager and the metadata manager I think it is a bad idea, lets keep concerns separated (I think we even should move the validation to another class)

Change validate method using a generic config array.

PHPBB3-15190
Add @internal warning to all validate_ methods

PHPBB3-15190
$this->metadata_file = $ext_path . 'composer.json';

// Initialize validations
$this->validations = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a static array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Didn't do it as I was not sure it was ok for the minimum PHP version.
Will do it tomorrow.

@javiexin
Copy link
Contributor Author

Three commits:

  1. New validate method (will change the array initialization to static)
  2. Include the "internal" message into the docblocks, please review if ok
  3. New method for version_check (maybe it should be a separate PR?)
    Note that this is not yet 100% complete: I want to review the uses of check_version from acp_extension and check CLI command, and may want to adapt to use the new validation option (will work as is, but might be better to update it).

Regarding merging extension manager and metadata manager, I think I miss-explained what I meant.
I do NOT want to merge both. I just want to make any direct access to metadata manager redundant, as it may be accessed through the extension manager as a proxy for md mgr. They are always used together, so why not use just one as the single interface? Also, having the metadata cached (lazy loaded) in extension manager as we have it now, it is clean to access it that way. Anyhow, definitely, a separate PR would be required.

@javiexin
Copy link
Contributor Author

@Nicofuma completed what we talked about:

  • validations array is now static (accessed through self::, maybe we should use static:: instead?)
  • validate_version_check is also complete, and cleaned the uses of it in acp_extensions and cli/check, plus the use in ext_manager that was already completed

Anything else you think is worth doing here?

About making the validation a new class... I don't think so: it is heavily mixed with the getter methods for metadata elements. But maybe we could create a trait as a design pattern for validation, that just entering the array as static would validate in any of the three available ways: string pattern, function and combined, or even think of more validation methods...

Complete the use of version check validation

PHPBB3-15190
@javiexin
Copy link
Contributor Author

@Nicofuma Anything else left to do here? Is this going forward?

@marc1706 marc1706 added this to the 3.3.0-a1 milestone Oct 29, 2017
@marc1706 marc1706 changed the base branch from 3.2.x to master October 29, 2017 11:15
@marc1706 marc1706 modified the milestones: 4.0.0-a1, 4.0.0-a2 Sep 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants