Merge pull request #1279 from roots/phpcs

[8.0.0] Coding standards, run PHP_CodeSniffer on Travis
This commit is contained in:
Ben Word
2015-01-24 10:20:36 -06:00
11 changed files with 86 additions and 32 deletions

View File

@@ -2,12 +2,17 @@ sudo: false
language: php
cache: npm
php:
- 5.6
- 5.5
- 5.4
- '5.6'
- '5.5'
- '5.4'
before_install:
- npm install -g bower
- npm install
- pyrus install pear/PHP_CodeSniffer
- phpenv rehash
script:
- npm run build
- npm run jshint
- phpcs --standard=ruleset.xml --extensions=php -n -s .

View File

@@ -1,6 +1,7 @@
### 8.0.0: TBD
* Change theme name from Roots to Sage
* Bump required PHP version to >=5.4
* Add coding standards based on PSR-2
* Add namespace
* Use short array syntax
* Use short echo syntax

View File

@@ -1,8 +1,10 @@
<?php
namespace Roots\Sage;
use Roots\Sage\Config;
use Roots\Sage\Wrapper;
?>
<?php get_template_part('templates/head'); ?>

View File

@@ -93,7 +93,7 @@ function google_analytics() {
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php else: ?>
<?php else : ?>
function ga() {
if (window.console) {
console.log('Google Analytics: ' + [].slice.call(arguments));

View File

@@ -1,6 +1,7 @@
<?php
namespace Roots\Sage\Config;
use Roots\Sage\Sidebar;
/**
@@ -41,7 +42,7 @@ function display_sidebar() {
static $display;
if (!isset($display)) {
$sidebar_config = new Sidebar\Sage_Sidebar(
$sidebar_config = new Sidebar\SageSidebar(
/**
* Conditional tag checks (http://codex.wordpress.org/Conditional_Tags)
* Any of these conditional tags that return true won't show the sidebar

View File

@@ -9,15 +9,16 @@ namespace Roots\Sage\Nav;
* <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="/">Home</a></li>
* <li id="menu-item-9" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-9"><a href="/sample-page/">Sample Page</a></l
*
* Sage_Nav_Walker example output:
* SageNavWalker example output:
* <li class="menu-home"><a href="/">Home</a></li>
* <li class="menu-sample-page"><a href="/sample-page/">Sample Page</a></li>
*/
class Sage_Nav_Walker extends \Walker_Nav_Menu {
function check_current($classes) {
class SageNavWalker extends \Walker_Nav_Menu {
public function checkCurrent($classes) {
return preg_match('/(current[-_])|active|dropdown/', $classes);
}
// @codingStandardsIgnoreStart
function start_lvl(&$output, $depth = 0, $args = []) {
$output .= "\n<ul class=\"dropdown-menu\">\n";
}
@@ -29,11 +30,9 @@ class Sage_Nav_Walker extends \Walker_Nav_Menu {
if ($item->is_dropdown && ($depth === 0)) {
$item_html = str_replace('<a', '<a class="dropdown-toggle" data-toggle="dropdown" data-target="#"', $item_html);
$item_html = str_replace('</a>', ' <b class="caret"></b></a>', $item_html);
}
elseif (stristr($item_html, 'li class="divider')) {
} elseif (stristr($item_html, 'li class="divider')) {
$item_html = preg_replace('/<a[^>]*>.*?<\/a>/iU', '', $item_html);
}
elseif (stristr($item_html, 'li class="dropdown-header')) {
} elseif (stristr($item_html, 'li class="dropdown-header')) {
$item_html = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $item_html);
}
@@ -50,6 +49,7 @@ class Sage_Nav_Walker extends \Walker_Nav_Menu {
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
// @codingStandardsIgnoreEnd
}
/**
@@ -82,7 +82,7 @@ add_filter('nav_menu_item_id', '__return_null');
* Clean up wp_nav_menu_args
*
* Remove the container
* Use Sage_Nav_Walker() by default
* Use SageNavWalker() by default
*/
function nav_menu_args($args = '') {
$nav_menu_args = [];

View File

@@ -14,25 +14,25 @@ namespace Roots\Sage\Sidebar;
*
* @return boolean True will display the sidebar, False will not
*/
class Sage_Sidebar {
class SageSidebar {
private $conditionals;
private $templates;
public $display = true;
function __construct($conditionals = [], $templates = []) {
public function __construct($conditionals = [], $templates = []) {
$this->conditionals = $conditionals;
$this->templates = $templates;
$conditionals = array_map([$this, 'check_conditional_tag'], $this->conditionals);
$templates = array_map([$this, 'check_page_template'], $this->templates);
$conditionals = array_map([$this, 'checkConditionalTag'], $this->conditionals);
$templates = array_map([$this, 'checkPageTemplate'], $this->templates);
if (in_array(true, $conditionals) || in_array(true, $templates)) {
$this->display = false;
}
}
private function check_conditional_tag($conditional_tag) {
private function checkConditionalTag($conditional_tag) {
$conditional_arg = is_array($conditional_tag) ? $conditional_tag[1] : false;
$conditional_tag = $conditional_arg ? $conditional_tag[0] : $conditional_tag;
@@ -43,7 +43,7 @@ class Sage_Sidebar {
}
}
private function check_page_template($page_template) {
private function checkPageTemplate($page_template) {
return is_page_template($page_template);
}
}

View File

@@ -10,14 +10,14 @@ namespace Roots\Sage\Wrapper;
*/
function template_path() {
return Sage_Wrapping::$main_template;
return SageWrapping::$main_template;
}
function sidebar_path() {
return new Sage_Wrapping('templates/sidebar.php');
return new SageWrapping('templates/sidebar.php');
}
class Sage_Wrapping {
class SageWrapping {
// Stores the full path to the main template file
public static $main_template;
@@ -28,7 +28,7 @@ class Sage_Wrapping {
public $templates;
// Stores the base name of the template file; e.g. 'page' for 'page.php' etc.
static $base;
public static $base;
public function __construct($template = 'base.php') {
$this->slug = basename($template, '.php');
@@ -45,7 +45,7 @@ class Sage_Wrapping {
return locate_template($this->templates);
}
static function wrap($main) {
public static function wrap($main) {
// Check for other filters returning null
if (!is_string($main)) {
return $main;
@@ -58,7 +58,7 @@ class Sage_Wrapping {
self::$base = false;
}
return new Sage_Wrapping();
return new SageWrapping();
}
}
add_filter('template_include', [__NAMESPACE__ . '\\Sage_Wrapping', 'wrap'], 99);
add_filter('template_include', [__NAMESPACE__ . '\\SageWrapping', 'wrap'], 99);

45
ruleset.xml Normal file
View File

@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<ruleset name="Roots">
<description>Roots Coding Standards</description>
<!-- Use PSR-2 as a base -->
<rule ref="PSR2">
<!-- Allow closing braces for functions and classes to be on the same line -->
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
<exclude name="Squiz.WhiteSpace.ScopeClosingBrace"/>
<!-- Disable newline after opening brace -->
<exclude name="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace"/>
<!-- Allow multiple PHP statements in the same line (usually in template files) -->
<exclude name="Generic.Formatting.DisallowMultipleStatements.SameLine"/>
<!-- Disable PSR-2 indentation rules that are buggy with 2 spaces -->
<exclude name="PSR2.ControlStructures.SwitchDeclaration.BreakIndent"/>
<exclude name="PSR2.Methods.FunctionCallSignature.Indent"/>
</rule>
<!-- Don't require a blank line after the last `use` in templates/ directory -->
<rule ref="PSR2.Namespaces.UseDeclaration.SpaceAfterLastUse">
<exclude-pattern>templates/*</exclude-pattern>
</rule>
<!-- Allow PHP closing tags on templates -->
<rule ref="Zend.Files.ClosingTag.NotAllowed">
<exclude-pattern>templates/*</exclude-pattern>
<exclude-pattern>404.php</exclude-pattern>
<exclude-pattern>index.php</exclude-pattern>
<exclude-pattern>page.php</exclude-pattern>
<exclude-pattern>single.php</exclude-pattern>
<exclude-pattern>template-custom.php</exclude-pattern>
</rule>
<!-- Force 2 spaces indentation -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="2"/>
<property name="tabIndent" value="false"/>
</properties>
</rule>
</ruleset>

View File

@@ -1,7 +1,7 @@
<?php
if (post_password_required()) {
return;
}
if (post_password_required()) {
return;
}
?>
<section id="comments" class="comments">

View File

@@ -14,9 +14,9 @@
<nav class="collapse navbar-collapse" role="navigation">
<?php
if (has_nav_menu('primary_navigation')) :
wp_nav_menu(['theme_location' => 'primary_navigation', 'walker' => new Nav\Sage_Nav_Walker(), 'menu_class' => 'nav navbar-nav']);
endif;
if (has_nav_menu('primary_navigation')) :
wp_nav_menu(['theme_location' => 'primary_navigation', 'walker' => new Nav\SageNavWalker(), 'menu_class' => 'nav navbar-nav']);
endif;
?>
</nav>
</div>