-
Notifications
You must be signed in to change notification settings - Fork 383
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
Added autoloader to reduce complexity; fix phpcs issues #828
Conversation
…_load_classes() and amp_load_fasterimage_classes(), created /include/templates/ directory for template-related classes, and Options Manager out of the views subdirectory.
…) does not accept a 2nd parameter.
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.
@mikeschinkel Thanks, I think this makes sense. A few changes needed before merging.
.gitmodules
Outdated
[submodule "dev-lib"] | ||
path = dev-lib | ||
url = https://github.com/xwp/wp-dev-lib.git | ||
branch = master |
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 file needs to be restored.
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.
I deleted it because it was blocking my ability to commit and I thought my pull request would not have included the deletion. I am not exactly sure what I was doing wrong or how to fix it (I gave up on submodules long ago as I always had issues with them.)
Is there any chance that you can accept the pull request after I fix everything else, and then restore manually?
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.
The lack of the submodule is causing Travis CI to fail: https://travis-ci.org/Automattic/amp-wp/jobs/313361694#L186
So it needs to be restored before we merge the PR so that the automated tests and checks can be performed.
What is the error you're having?
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.
Let me see if I can figure it out.
I was using SourceTree to allow me to make partial commits of files and it said I needed the submodule but was locked in the dialog and would not let me exit. When I deleted the file SourceTree recognized the change and immediately commit my changes.
I will try reverting this commit and see if I can get things to work from the command line. If I have an issue I will let you know otherwise I will resolve it and we can just move forward.
autoloader.php
Outdated
* | ||
* @return AMP_Autoloader | ||
*/ | ||
public static function instance() { |
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.
There aren't any singletons in this plugin, so I think static methods should be used instead for consistency.
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.
Funny, I debated using static but assumed you'd prefer instances. :-)
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.
Normally I'd prefer non-singleton instances, but given most classes are just being used as namespaces with static methods, using static here again seems to be most in keeping with everything else.
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.
Updated to use static methods.
autoloader.php
Outdated
public static function register() { | ||
static $registered = false; | ||
if ( ! $registered ) { | ||
spl_autoload_register( [ static::instance(), '_autoload' ] ); |
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.
Syntax error in PHP 5.2
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.
5.2? :-o
Fixed.
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.
Yeah. Currently the plugin supports PHP 5.2: https://github.com/Automattic/amp-wp/blob/eeb502b28807c1be80e59937fab6112e406b204b/.travis.yml#L45-L47
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.
Noted.
autoloader.php
Outdated
AMP_Autoloader::register(); | ||
|
||
|
||
|
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.
Extra linebreaks at end of file.
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.
Fixed.
includes/admin/functions.php
Outdated
@@ -106,6 +106,11 @@ function amp_add_options_menu() { | |||
} | |||
add_action( 'wp_loaded', 'amp_add_options_menu' ); | |||
|
|||
/** |
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.
Missing function description, param description, and return description. We've also been adding @since
tags.
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.
I do not understand that function's parameter well enough to fully document so I backed out the partial PHPDoc I had added.
includes/amp-helper-functions.php
Outdated
@@ -1,5 +1,10 @@ | |||
<?php | |||
|
|||
/** |
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.
Missing function description, param description, and return description. We've also been adding @since
tags.
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.
Your requests to document this caused me to go down the rabbit hole for the past (far too many) hours. Every time I would document something it would cause PhpStorm to flag something else as being "off", so I ended up documented all of the sanitizers.
Note I found a few places that would throw errors, e.g. DOMNode $nodes
using methods that are only on DOMElement
so I added a variable of logic to ensure errors are not thrown during the normal course of usage when WP_DEBUG
is set to true
. One such example is I added the following in several places:
if ( ! $node instanceof DOMElement ) {
return;
}
I hope that was okay...
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.
The node type checks are probably a good idea, as I can see that clearly some of the situations would result in text nodes being used as elements. It looks like some of the instances you added would never result in a non-element being iterated (e.g. $this->dom->getElementsByTagName( self::$tag );
) but it doesn't hurt.
@@ -73,6 +71,9 @@ public function sanitize() { | |||
} | |||
} | |||
|
|||
/** |
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.
Missing function description, param description. We've also been adding @since
tags.
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.
Documented.
…re else is using the leading underscore convention for private methods.
…ew-clarity-develop
We kinda opened a can of worms here because now there are many PHPCS violations being reported, because of the various files being touched, and wp-dev-lib will only report PHPCS errors surrounding actual patches. The PHPCS issues need to be fixed at some point anyway, but I don't know if now is the time. I am noticing, however, one PHPUnit failure that must be fixed:
|
@westonruter I have been trying to recreate a local PHPUnit setup to run the tests locally and while I've gotten past numerous issues I've found a roadblock on this error message: It appears to be requiring an Google is failing me to find the right solution to this. I'm sure I could power through it given enough time but I am hoping you can make me more productive and help me know how to solve this one. Help? |
@mikeschinkel Yeah, there's a couple options here. For one, you can add the following to your export WP_TESTS_DIR=~/path/to/wordpress-develop/tests/phpunit/ That assumes you have a fully up-and-running wordpress-develop core unit tests on the machine. Otherwise, you can just run the unit tests inside of VVV. You can see that it has this out of the box: https://github.com/Varying-Vagrant-Vagrants/VVV/blob/5b4ee4b/config/bash_profile#L29-L33 |
@westonruter Thanks. That got me past that roadblock. However the next roadblock is trying to use either PHPUnit
Clearly I have a later version of With PHPUnit
When I switch to PHPUnit |
@westonruter Good news! I was about to get all |
…breaking unit tests.)
By did you need to add Composer to get it to work? In any case, it makes sense to include. I think it could look like https://github.com/WordPress/better-code-editing/blob/master/composer.json |
…ia raw $dom->saveXML().
@westonruter Yes, because I was trying to use PhpStorm to run the tests (as it provides such a nice interface) and for some reason PhpStorm would not recognize the I added one without |
…e() in AMP_DOM_Utils.
…g tests work correctly.
…yet and thus is causing the build to fail.
… build. (Methinks we would need to update the composer.json file for each Travis build to support the proper version of PHP?)
@westonruter Finally! What a rabbit hole that was. :-) Let me know if you have an issues otherwise I will start working on how to implement a full theme in AMP and run the ideas by you either in issue #827 or via a new pull request. |
public function get_styles() { | ||
if ( ! $this->did_convert_elements ) { |
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 this if $this->styles
is already an array()
?
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.
Consistency. But to your point I guess it is not needed.
Woohoo! :-D |
@westonruter As I started working on a refactoring as per here I found myself struggling with the complexity of all the classes and making sure they were all loaded in the correct order. So I went out on a limb and implemented a simple, fast classmap autoloader and removed all the now-unnecessary
require_once()
calls throughout the plugin. I am really hoping you will see this as a plus and accept it, and then I can move forward with the next step of refactor as in the issue above.While I was at it PhpStorm pointed out several errors and oversights and I created an individual commit for each of those, but the first commit has everything needed for and related to the autoloader.