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

Add PHP coding standards #19

Merged
merged 5 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions app/App.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
<?php
/**
* Class to initialize the application
*/

namespace DeliciousBrains\SpinupWPComposerSite;

/**
* App class to initialize the application
*/
class App {

/**
* Load custom site code
* Load custom site code
*/
public function register() {
}

/**
* Helper for defining constants if not already defined.
*
* @param string $key
* @param mixed $value
* @param string $key The name of the constant to define
* @param mixed $value The value to give the constant
*/
public static function define( $key, $value ) {
if ( defined( $key ) ) {
Expand All @@ -25,8 +31,10 @@ public static function define( $key, $value ) {
}

/**
* @param $name
* @param $arguments
* This allows the environment to be queried using magic methods like App::is_env_<environment>
*
* @param string $name The name of the magic method
* @param array $arguments The arguments to the magic method
*
* @return bool
*/
Expand All @@ -36,4 +44,4 @@ public static function __callStatic( $name, $arguments ) {
return $env === env( 'WP_ENV' );
}
}
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"sort-packages": true,
"allow-plugins": {
"composer/installers": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
Expand Down Expand Up @@ -52,5 +53,8 @@
},
"autoload": {
"psr-4": {"DeliciousBrains\\SpinupWPComposerSite\\": "app/"}
},
"require-dev": {
"humanmade/coding-standards": "^1.1"
}
}
41 changes: 20 additions & 21 deletions config/app.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Your base production configuration goes in this file. Environment-specific
* overrides go in their respective config/environments/{{WP_ENV}}.php file.
Expand All @@ -9,7 +8,7 @@
* can.
*/

use \DeliciousBrains\SpinupWPComposerSite\App;
use DeliciousBrains\SpinupWPComposerSite\App;

$root_dir = dirname( __DIR__ );
$webroot_dir = $root_dir . '/public';
Expand All @@ -23,7 +22,7 @@
* Use Dotenv to set required environment variables and load .env file in root
*/
$env_files = file_exists( $root_dir . '/.env.local' ) ? [ '.env', '.env.local' ] : [ '.env' ];
$dotenv = Dotenv\Dotenv::createUnsafeImmutable($root_dir, $env_files, false);
$dotenv = Dotenv\Dotenv::createUnsafeImmutable( $root_dir, $env_files, false );
if ( file_exists( $root_dir . '/.env' ) ) {
$dotenv->load();
$dotenv->required( [ 'DB_NAME', 'DB_USER', 'DB_PASSWORD', 'WP_HOME', 'WP_SITEURL' ] );
Expand All @@ -40,36 +39,36 @@
App::define( 'WP_CONTENT_DIR', $webroot_dir . CONTENT_DIR );
App::define( 'WP_CONTENT_URL', WP_HOME . CONTENT_DIR );

App::define('DB_NAME', env('DB_NAME'));
App::define('DB_USER', env('DB_USER'));
App::define('DB_PASSWORD', env('DB_PASSWORD'));
App::define('DB_HOST', env('DB_HOST') ?: 'localhost');
App::define('DB_CHARSET', 'utf8mb4');
App::define('DB_COLLATE', '');
$table_prefix = env('DB_PREFIX') ?: 'wp_';
App::define( 'DB_NAME', env( 'DB_NAME' ) );
App::define( 'DB_USER', env( 'DB_USER' ) );
App::define( 'DB_PASSWORD', env( 'DB_PASSWORD' ) );
App::define( 'DB_HOST', env( 'DB_HOST' ) ?: 'localhost' );
App::define( 'DB_CHARSET', 'utf8mb4' );
App::define( 'DB_COLLATE', '' );
$table_prefix = env( 'DB_PREFIX' ) ?: 'wp_';

// Set other constants from env file.
foreach( array_keys($dotenv->load()) as $key ) {
App::define($key, env( $key ));
foreach ( array_keys( $dotenv->load() ) as $key ) {
App::define( $key, env( $key ) );
}

// Environment config
// Environment config.
$env_config = __DIR__ . '/environments/' . WP_ENV . '.php';
if ( file_exists( $env_config ) ) {
require_once $env_config;
}

App::define('AUTOMATIC_UPDATER_DISABLED', true);
App::define('DISABLE_WP_CRON', env('DISABLE_WP_CRON') ?: false);
// Disable the plugin and theme file editor in the admin
App::define('DISALLOW_FILE_EDIT', true);
// Disable plugin and theme updates and installation from the admin
App::define('DISALLOW_FILE_MODS', true);
App::define( 'AUTOMATIC_UPDATER_DISABLED', true );
App::define( 'DISABLE_WP_CRON', env( 'DISABLE_WP_CRON' ) ?: false );
// Disable the plugin and theme file editor in the admin.
App::define( 'DISALLOW_FILE_EDIT', true );
// Disable plugin and theme updates and installation from the admin.
App::define( 'DISALLOW_FILE_MODS', true );

// Secret keys
// Secret keys.
if ( ! file_exists( __DIR__ . '/keys.php' ) ) {
$keys = file_get_contents( 'https://api.wordpress.org/secret-key/1.1/salt/' );
file_put_contents( __DIR__ . '/keys.php', '<?php use ' . App::class . '; ' . str_replace( 'define(', 'App::define(', $keys ) );
file_put_contents( __DIR__ . '/keys.php', '<?php use ' . App::class . ";\n" . str_replace( 'define(', 'App::define(', $keys ) );
}
include __DIR__ . '/keys.php';

Expand Down
32 changes: 32 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<ruleset name="Delicious Brains">
<!-- Files or directories to check -->
<file>.</file>

<!-- Exclude the keys file as this is auto-generated from the WordPress generator -->
<exclude-pattern type="relative-root">^/config/keys.php</exclude-pattern>

<!-- Path to strip from the front of file paths inside reports (displays shorter paths) -->
<arg name="basepath" value="." />

<!-- Use HM Coding Standards -->
<rule ref="vendor/humanmade/coding-standards">
<!-- Namespace isn't required for all files. -->
<exclude name="HM.Functions.NamespacedFunctions.MissingNamespace" />
<!-- Ignore rule expecting Namespaced directory. -->
<exclude name="HM.Files.NamespaceDirectoryName.NoIncDirectory" />
<!-- File name and class name match is not necessary. -->
<exclude name="HM.Files.ClassFileName.MismatchedName" />
<!-- Ignore class file name rule -->
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
<!-- Ignore rule expecting hyphens in file name. -->
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
<!-- Don't require file comment header. -->
<exclude name="Squiz.Commenting.FileComment.Missing" />
</rule>

<!-- Ignore Snake case variables for tests -->
<rule ref="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase">
<exclude-pattern>/tests/*</exclude-pattern>
</rule>
</ruleset>
7 changes: 5 additions & 2 deletions public/content/mu-plugins/app.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?php
// Bootstrap our site code
( new DeliciousBrains\SpinupWPComposerSite\App() )->register();
/**
* Bootstrap our site code
*/

( new DeliciousBrains\SpinupWPComposerSite\App() )->register();
4 changes: 2 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @var bool
*/
define('WP_USE_THEMES', true);
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
2 changes: 1 addition & 1 deletion public/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
require_once dirname( __DIR__ ) . '/config/app.php';
require_once ABSPATH . 'wp-settings.php';
require_once ABSPATH . 'wp-settings.php';