Skip to content
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

Use email address validator from Zend #8259

Merged
merged 6 commits into from Jul 6, 2015
Merged

Use email address validator from Zend #8259

merged 6 commits into from Jul 6, 2015

Conversation

barbushin
Copy link
Contributor

Closes #7298

@barbushin barbushin added the Needs Review PRs that need a code review label Jul 1, 2015
@mattab
Copy link
Member

mattab commented Jul 1, 2015

Feedback:

@barbushin
Copy link
Contributor Author

@mattab Done

'mx' => false,
'deep' => false
));
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using a static variable here, DI should be used. You can get an object from DI via,

StaticContainer::get('Zend_Validate_EmailAddress'); // full class name goes here

If there's no definition in a PHP config file (ie, config/global.php or one of the plugin ones), the DI container will create a new instance itself, autowiring dependencies.

Since you're creating a instance w/ a specific constructor arg, you'll have to add a definition for the instance in config/global.php. Should be self-explanatory how to do it, let me know if it isn't.

Regarding StaticContainer use: since Piwik's internals are so deeply coupled, we have to use something like this (which stores the container as a global static variable), however we're getting close to removing it. In the future, if possible, try not to use it (by specifying dependencies in the constructor). In this case, since it's a static method, there's no choice but to use it (especially since it's @api). If this isn't clear, let me know, I'll try to clarify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@diosmosis StaticContainer is martked as @deprecated.

Copy link
Member

Choose a reason for hiding this comment

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

It was introduced as @deprecated because Piwik's dependencies are too hard-coded. It's taken us ~5 months or so to try to remove it, and we are still one month away from doing it. You can use it for now, though you should try to avoid where possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@diosmosis I'm worried, that if I put Zend_Validate_EmailAddress in DI, then another core/plugins developers will call email validation using Zend_Validate_EmailAddress from DI, instead of using Piwik::isValidEmailString(), and in this way Piwik will depends on Zend/Validate library. I gues this case is not about DI pattern, it's about https://en.wikipedia.org/wiki/Flyweight_pattern

But if this is a common practice for Piwik to use DI container as Flyweighter, then I can do some refactoring. Benaka, are you 100% sure about it?

Copy link
Member

Choose a reason for hiding this comment

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

Most of what's in DI is currently not API, and when things are made API they are explicitly mentioned in docs (or should be).

As for the flyweight pattern, that normally involves more than one object. This is caching a single instance for the current request. If you don't want to use DI, you could use the transient cache, via, Piwik\Cache::getTransientCache().

Whatever you do, please make sure this value can be reset during tests, and if possible, overridden (though I think this is only do-able through DI). Static local variables have made me waste days debugging tests, and I would gladly anger the design pattern gods or whatever entity is responsible for programming zealotry if it meant I didn't have to waste my time on pointless work.

@barbushin
Copy link
Contributor Author

@diosmosis I've renamed test methods, and changed test parent class to PHPUnit_Framework_TestCase. Thanks for that review!

@barbushin
Copy link
Contributor Author

Moved Zend_Validate_EmailAddress to DI. Now it's ready to merge?

Mow email validation with different email formats is tested in EmailValidatorTest. UsersManagerTest::testAddUserWrongEmail() test is just about exception throwing.
@barbushin
Copy link
Contributor Author

It was a wrong old test method UsersManagerTest::testAddUserWrongEmail() that asserted that ema'il@email.com is invalid, but regarding RFC and new email validator(and http://isemail.info) ema'il@email.com is valid.

array('mx' => false, 'deep' => false) are default settings for Zend_Validate_EmailAddress so I think we can just forget about it.

@diosmosis diosmosis added this to the 2.14.0 milestone Jul 6, 2015
@diosmosis diosmosis added the not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. label Jul 6, 2015
diosmosis added a commit that referenced this pull request Jul 6, 2015
Fixes #7298, use email address validator from Zend in Piwik::isValidEmail.
@diosmosis diosmosis merged commit 80b8743 into matomo-org:master Jul 6, 2015
diosmosis pushed a commit that referenced this pull request Jul 6, 2015
…ince we don't need to specify custom constructor parameters. php-di will just default to default construction.
'Piwik\EventDispatcher' => DI\object()->constructorParameter('observers', DI\get('observers.global'))
'Piwik\EventDispatcher' => DI\object()->constructorParameter('observers', DI\get('observers.global')),

'Zend_Validate_EmailAddress' => DI\object('Zend_Validate_EmailAddress'),
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI this line is not necessary: by default with autowiring PHP-DI can create the instance without configuration needed (because here there is no constructor parameter to configure). It's no big deal but it's just to let you know

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I just saw d4a024b ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants