Skip to content

Commit

Permalink
Migration to Human Made coding standards [WIP] (#758)
Browse files Browse the repository at this point in the history
* Initial migration to Human Made coding standards.
* Add autoloader to tests/boostrap.php.
* Add autoloader to Travis.
* Bundle autoloader.
* Fix many tests.
* Fix coverage paths.
* Update phpcs.ruleset.xml.
* Exclude specific files from rule.
* Fix false positives for isFormSubmission.
* Fixes for HM coding standards.
* Point to new inc/ directory
* Change isFormSubmission() to better conform to coding standards.
* Reduce @codingStandardsIgnore
* Trick coding standards,
* Last call.
* Mr clean.
* Mrs Clean.
* Add more files to scan.
* Fix spacing/formatting to better match HM standards.
* Increase code coverage.
* Remove @Covers to fix under reporting in PHPStorm, ran phpcbf.
* Add editor tests.
* Add sanitize test.
* Add utility test.
* Fix for HM standards.
* Fix coding standards error.
  • Loading branch information
Ned Zimmerman committed May 30, 2017
1 parent 62cba0e commit 005dccf
Show file tree
Hide file tree
Showing 141 changed files with 5,242 additions and 5,982 deletions.
19 changes: 18 additions & 1 deletion .travis.yml
Expand Up @@ -15,11 +15,28 @@ php:
- 5.6
- 7.0
- 7.1
- nightly

env:
- WP_VERSION=latest WP_MULTISITE=1
- WP_VERSION=latest WP_MULTISITE=1 TRAVIS_NODE_VERSION="6"
- WP_VERSION=latest WP_MULTISITE=1 TRAVIS_NODE_VERSION="7"

matrix:
allow_failures:
- php: nightly

cache:
directories:
- $HOME/.composer/cache
- $HOME/.yarn-cache
- node_modules
- vendor

before_install:
- source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION && nvm use $TRAVIS_NODE_VERSION
- node -v

install:
- composer install

before_script:
Expand Down
46 changes: 46 additions & 0 deletions autoloader.php
@@ -0,0 +1,46 @@
<?php

namespace HM\Autoloader;

class Autoloader {
const NS_SEPARATOR = '\\';

protected $prefix;
protected $prefix_length;
protected $path;

public function __construct( $prefix, $path ) {
$this->prefix = $prefix;
$this->prefix_length = strlen( $prefix );
$this->path = trailingslashit( $path );
}

public function load( $class ) {
if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
return;
}

// Strip prefix from the start (ala PSR-4)
$class = substr( $class, $this->prefix_length + 1 );
$class = strtolower( $class );
$file = '';

if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
$namespace = substr( $class, 0, $last_ns_pos );
$class = substr( $class, $last_ns_pos + 1 );
$file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
}
$file .= 'class-' . str_replace( '_', '-', $class ) . '.php';

$path = $this->path . $file;

if ( file_exists( $path ) ) {
require_once $path;
}
}
}

function register_class_path( $prefix, $path ) {
$loader = new Autoloader( $prefix, $path );
spl_autoload_register( [ $loader, 'load' ] );
}
4 changes: 2 additions & 2 deletions bin/compile-scss.php
Expand Up @@ -23,7 +23,7 @@
// Sassify
// --------------------------------------------------------------------------------------------------------------------

$includePaths = [
$include_paths = [
__DIR__ . '/../assets/scss/partials',
__DIR__ . '/../assets/scss/fonts',
dirname( realpath( $input_file_name ) ),
Expand All @@ -34,7 +34,7 @@
try {
$sass = new \Leafo\ScssPhp\Compiler;
$sass->setStyle( Sass::STYLE_EXPANDED );
$sass->setImportPaths( $includePaths );
$sass->setImportPaths( $include_paths );
$css = $sass->compileFile( $scss );
} catch ( Exception $e ) {
die( $e->getMessage() );
Expand Down
12 changes: 5 additions & 7 deletions composer.json
Expand Up @@ -30,12 +30,12 @@
"pimple/pimple": "^3.0",
"leafo/scssphp": "^0.6.7",
"pressbooks/pb-api": "^1.1",
"perchten/rmrdir": "^1.0"
"perchten/rmrdir": "^1.0",
"sinergi/browser-detector": "^6.1"
},
"require-dev": {
"phpunit/phpunit": "~5.5",
"squizlabs/php_codesniffer": "^2.7",
"wp-coding-standards/wpcs": "^0.10"
"humanmade/coding-standards": "^0.2.1"
},
"archive" : {
"exclude": [
Expand All @@ -54,12 +54,10 @@
"scripts": {
"test": [
"vendor/bin/phpunit --configuration phpunit.xml --coverage-clover coverage.xml",
"vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs",
"vendor/bin/phpcs --standard=phpcs.xml --extensions=php ."
"@standards"
],
"standards": [
"vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs",
"vendor/bin/phpcs --standard=phpcs.xml --extensions=php ."
"vendor/bin/phpcs --standard=phpcs.ruleset.xml *.php inc/ bin/"
]
}
}
119 changes: 118 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 19 additions & 18 deletions hooks-admin.php
Expand Up @@ -12,15 +12,15 @@
// Includes
// -------------------------------------------------------------------------------------------------------------------

require( PB_PLUGIN_DIR . 'includes/admin/pb-dashboard.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-diagnostics.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-laf.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-plugins.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-analytics.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-metaboxes.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-customcss.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-network-managers.php' );
require( PB_PLUGIN_DIR . 'includes/admin/pb-fonts.php' );
require( PB_PLUGIN_DIR . 'inc/admin/dashboard/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/diagnostics/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/laf/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/plugins/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/analytics/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/metaboxes/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/customcss/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/networkmanagers/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/admin/fonts/namespace.php' );

// -------------------------------------------------------------------------------------------------------------------
// Look & feel of admin interface and Dashboard
Expand All @@ -43,8 +43,8 @@

if ( \Pressbooks\Book::isBook() ) {
// Aggressively replace default interface
add_action( 'init', array( '\Pressbooks\Modules\SearchAndReplace\SearchAndReplace', 'init' ) );
add_action( 'init', array( '\Pressbooks\Modules\ThemeOptions\ThemeOptions', 'init' ) );
add_action( 'init', [ '\Pressbooks\Modules\SearchAndReplace\SearchAndReplace', 'init' ] );
add_action( 'init', [ '\Pressbooks\Modules\ThemeOptions\ThemeOptions', 'init' ] );
add_action( 'admin_init', '\Pressbooks\Admin\Laf\redirect_away_from_bad_urls' );
add_action( 'admin_menu', '\Pressbooks\Admin\Laf\replace_book_admin_menu', 1 );
add_action( 'wp_dashboard_setup', '\Pressbooks\Admin\Dashboard\replace_dashboard_widgets' );
Expand All @@ -63,7 +63,7 @@
add_action( 'wp_network_dashboard_setup', '\Pressbooks\Admin\Dashboard\replace_network_dashboard_widgets' );
}

if ( true == is_main_site() ) {
if ( true === is_main_site() ) {
add_action( 'wp_dashboard_setup', '\Pressbooks\Admin\Dashboard\replace_root_dashboard_widgets' );
}

Expand Down Expand Up @@ -127,6 +127,7 @@
} );

add_action( 'custom_metadata_manager_init_metadata', '\Pressbooks\Admin\Metaboxes\add_meta_boxes' );
add_action( 'pb_add_bisac_subjects_field', '\Pressbooks\Admin\Metaboxes\add_bisac_subjects_field', 1 );

if ( \Pressbooks\Book::isBook() ) {
add_action( 'admin_enqueue_scripts', '\Pressbooks\Admin\Metaboxes\add_metadata_styles' );
Expand Down Expand Up @@ -192,7 +193,7 @@

// Posts, Meta Boxes
add_action( 'updated_postmeta', function ( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'pb_language' == $meta_key ) {
if ( 'pb_language' === $meta_key ) {
\Pressbooks\Book::deleteBookObjectCache();
\Pressbooks\Admin\Fonts\update_font_stacks();
}
Expand All @@ -203,9 +204,9 @@
add_action( 'admin_init', '\Pressbooks\Editor\add_editor_style' );

// Overrides
add_filter( 'pb_epub_css_override', array( '\Pressbooks\Modules\ThemeOptions\EbookOptions', 'scssOverrides' ) );
add_filter( 'pb_pdf_css_override', array( '\Pressbooks\Modules\ThemeOptions\PDFOptions', 'scssOverrides' ) );
add_filter( 'pb_web_css_override', array( '\Pressbooks\Modules\ThemeOptions\WebOptions', 'scssOverrides' ) );
add_filter( 'pb_epub_css_override', [ '\Pressbooks\Modules\ThemeOptions\EbookOptions', 'scssOverrides' ] );
add_filter( 'pb_pdf_css_override', [ '\Pressbooks\Modules\ThemeOptions\PDFOptions', 'scssOverrides' ] );
add_filter( 'pb_web_css_override', [ '\Pressbooks\Modules\ThemeOptions\WebOptions', 'scssOverrides' ] );
}

// Theme Lock
Expand Down Expand Up @@ -233,12 +234,12 @@

// Disable all pointers (i.e. tooltips) all the time, see \WP_Internal_Pointers()
add_action( 'admin_init', function () {
remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
remove_action( 'admin_enqueue_scripts', [ 'WP_Internal_Pointers', 'enqueue_scripts' ] );
} );

// Fix for "are you sure you want to leave page" message when editing a part
add_action( 'admin_enqueue_scripts', function () {
if ( 'part' == get_post_type() ) {
if ( 'part' === get_post_type() ) {
wp_dequeue_script( 'autosave' );
}
} );
Expand Down
26 changes: 13 additions & 13 deletions hooks.php
Expand Up @@ -12,17 +12,17 @@
// Includes
// -------------------------------------------------------------------------------------------------------------------

require( PB_PLUGIN_DIR . 'includes/admin/pb-branding.php' );
require( PB_PLUGIN_DIR . 'includes/pb-analytics.php' );
require( PB_PLUGIN_DIR . 'includes/pb-utility.php' );
require( PB_PLUGIN_DIR . 'includes/pb-image.php' );
require( PB_PLUGIN_DIR . 'includes/pb-l10n.php' );
require( PB_PLUGIN_DIR . 'includes/pb-posttype.php' );
require( PB_PLUGIN_DIR . 'includes/pb-redirect.php' );
require( PB_PLUGIN_DIR . 'includes/pb-registration.php' );
require( PB_PLUGIN_DIR . 'includes/pb-sanitize.php' );
require( PB_PLUGIN_DIR . 'includes/pb-media.php' );
require( PB_PLUGIN_DIR . 'includes/pb-editor.php' );
require( PB_PLUGIN_DIR . 'inc/admin/branding/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/analytics/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/utility/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/image/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/l10n/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/posttype/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/redirect/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/registration/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/sanitize/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/media/namespace.php' );
require( PB_PLUGIN_DIR . 'inc/editor/namespace.php' );

Pressbooks\Utility\include_plugins();

Expand Down Expand Up @@ -167,8 +167,8 @@

// Theme Lock
if ( \Pressbooks\Book::isBook() && \Pressbooks\ThemeLock::isLocked() ) {
add_filter( 'pb_stylesheet_directory', array( '\Pressbooks\ThemeLock', 'getLockDir' ) );
add_filter( 'pb_stylesheet_directory_uri', array( '\Pressbooks\ThemeLock', 'getLockDirURI' ) );
add_filter( 'pb_stylesheet_directory', [ '\Pressbooks\ThemeLock', 'getLockDir' ] );
add_filter( 'pb_stylesheet_directory_uri', [ '\Pressbooks\ThemeLock', 'getLockDirURI' ] );
}

// -------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 005dccf

Please sign in to comment.