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

Separate declarations from side-effects #7

Closed
szepeviktor opened this issue Sep 30, 2020 · 6 comments
Closed

Separate declarations from side-effects #7

szepeviktor opened this issue Sep 30, 2020 · 6 comments

Comments

@szepeviktor
Copy link
Contributor

szepeviktor commented Sep 30, 2020

  1. move the main class to lib/Underpin.php
  2. move underpin() to lib/helpers.php
  3. autoload wordpress-autoloader.php and lib/helpers.php with Composer
  4. Provide an example of loading Underpin - see below
  5. remove autoloading from abstracts\Underpin
require_once __DIR_ . '/vendor/autoload.php';

add_action( 'plugins_loaded', '\\Underpin\\underpin' );
@szepeviktor
Copy link
Contributor Author

szepeviktor commented Sep 30, 2020

wordpress-autoloader.php

<?php
/**
 * Autoloader for WordPress.
 *
 * @package Underpin
 * @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#naming-conventions
 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example
 */

declare( strict_types = 1 );

namespace Underpin;

use function spl_autoload_register;

spl_autoload_register(
	/**
	 * Closure of the autoloader.
	 *
	 * @param class-string $class_name The fully-qualified class name.
	 * @return void
	 */
	static function ( $class_name ) {
		$project_namespace = __NAMESPACE__ . '\\';
		$length            = strlen( $project_namespace );

		// Class is not in our namespace.
		if ( 0 !== strncmp( $project_namespace, $class_name, $length ) ) {
			return;
		}

		// E.g. Model\Item
		$relative_class_name = substr( $class_name, $length );
		// E.g. model/Item.php
		$name_parts = explode( '\\', $relative_class_name );
		$last_part  = array_pop( $name_parts );

		$file = sprintf(
			'%1$s/lib%2$s/%3$s.php',
			__DIR__,
			array() === $name_parts ? '' : '/' . strtolower( str_replace( '_', '-', implode( '/', $name_parts ) ) ),
			$last_part
		);

		if ( ! is_file( $file ) ) {
			return;
		}

		require $file;
	}
);

Original: https://github.com/szepeviktor/debian-server-tools/blob/master/webserver/wp-install/wordpress-autoloader.php

@szepeviktor
Copy link
Contributor Author

We currently have a mix of PSR-4 class file names and WPCS directory names.

@szepeviktor szepeviktor changed the title Separate declaration from side-effects Separate declarations from side-effects Sep 30, 2020
@szepeviktor
Copy link
Contributor Author

Going further: underpin()-> may be replaced with a DI container

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Sep 30, 2020

After everything is clean and tidy: phpstan.neon.dist

includes:
    - vendor/szepeviktor/phpstan-wordpress/extension.neon
parameters:
#    level: max
    level: 5
    inferPrivatePropertyTypeFromConstructor: true
    paths:
        - lib/
    ignoreErrors:
        # Uses func_get_args()
        - '#^Function apply_filters(_ref_array)? invoked with [34567] parameters, 2 required\.$#'

[ERROR] Found 74 errors

Will keep you entertained for 2 hours.

@alexstandiford
Copy link
Collaborator

or be old-bold and use updated WPCS everywhere :) https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/

Nope. This plugin is unapologetic-ally intended to avoid that. To me, if you want to use WPCS, use WordPress.

I appreciate the information in this, would it be possible to have a PR submitted? That would make it way easier to understand the differences, and test.

@szepeviktor
Copy link
Contributor Author

Done.

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 a pull request may close this issue.

2 participants