Skip to content

Commit c4f157d

Browse files
authored
Merge pull request #23 from lloc/code-style
Code style
2 parents ee0e459 + 41804b6 commit c4f157d

11 files changed

Lines changed: 284 additions & 162 deletions

File tree

.distignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ coverage.xml
1212
.gitignore
1313
.phpunit.result.cache
1414
.scrutinizer.yml
15-
Changelog.md
16-
composer.json
1715
composer.lock
1816
mslsmenu.zip
1917
package.json
2018
package-lock.json
2119
phpdoc.xml
22-
phpstan.neon
20+
.phpstan.neon.dist
21+
.phpcs.cache
22+
.phpcs.xml.dist
2323
phpunit.xml
2424
README.md

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
.phpunit.result.cache
3+
.phpcs.cache
34
.idea/
45
vendor/
56
reports/

.phpcs.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="CS">
3+
<description>PHPCS MslsMenu configuration</description>
4+
<config name="testVersion" value="7.4-"/>
5+
<exclude-pattern>bin/*</exclude-pattern>
6+
<exclude-pattern>tests/*</exclude-pattern>
7+
<exclude-pattern>vendor/*</exclude-pattern>
8+
9+
<arg value="ps"/>
10+
<arg name="colors"/>
11+
<arg name="parallel" value="100"/>
12+
<arg name="extensions" value="php"/>
13+
<arg name="cache" value=".phpcs.cache"/>
14+
15+
<rule ref="WordPress">
16+
<exclude name="Generic.Commenting.DocComment.MissingShort" />
17+
<exclude name="Squiz.Commenting.FileComment.Missing" />
18+
<exclude name="Squiz.Commenting.ClassComment.Missing" />
19+
<exclude name="Squiz.Commenting.FunctionComment.Missing" />
20+
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment" />
21+
<exclude name="WordPress.Files.FileName" />
22+
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.Found" />
23+
</rule>
24+
<rule ref="WordPress-Extra"/>
25+
<rule ref="WordPress-Docs"/>
26+
</ruleset>
File renamed without changes.

MslsMenu.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @copyright Copyright (C) 2011-2022, Dennis Ploetner, re@lloc.de
66
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 or later
77
* @wordpress-plugin
8+
* @package mslsmenu
89
*
910
* Plugin Name: MslsMenu
1011
* Requires Plugins: multisite-language-switcher
@@ -35,6 +36,7 @@
3536

3637
/**
3738
* MslsMenu Class
39+
*
3840
* @package mslsmenu
3941
*/
4042
final class MslsMenu {
@@ -56,8 +58,6 @@ final class MslsMenu {
5658
*/
5759
public function __construct( $options ) {
5860
$this->options = $options;
59-
60-
load_plugin_textdomain( 'mslsmenu', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
6161
}
6262

6363
/**
@@ -71,21 +71,21 @@ public static function init( $options ): MslsMenu {
7171
$obj = new self( $options );
7272

7373
if ( ! is_null( $options ) ) {
74-
add_filter( 'wp_nav_menu_items', [ $obj, 'nav_item' ], 10, 2 );
75-
add_action( 'msls_admin_register', [ $obj, 'admin_register' ] );
74+
add_filter( 'wp_nav_menu_items', array( $obj, 'nav_item' ), 10, 2 );
75+
add_action( 'msls_admin_register', array( $obj, 'admin_register' ) );
7676
}
7777

7878
return $obj;
7979
}
8080

81-
private function get_msls_output(): lloc\Msls\MslsOutput{
81+
private function get_msls_output(): lloc\Msls\MslsOutput {
8282
return function_exists( 'msls_output' ) ? msls_output() : lloc\Msls\MslsOutput::init();
8383
}
8484

8585
/**
8686
* Callback for wp_nav_menu_items
8787
*
88-
* @param string $items
88+
* @param string $items
8989
* @param \stdClass $args
9090
*
9191
* @return string
@@ -94,7 +94,7 @@ public function nav_item( string $items, \stdClass $args ): string {
9494
$menu_locations = $this->options->mslsmenu_theme_location ?? '';
9595
$theme_location = $args->theme_location ?? '';
9696

97-
if ( is_array( $menu_locations ) && in_array( $theme_location, $menu_locations ) ) {
97+
if ( is_array( $menu_locations ) && in_array( $theme_location, $menu_locations, true ) ) {
9898
$menu = '';
9999

100100
$obj = $this->get_msls_output();
@@ -118,31 +118,31 @@ public function admin_register( string $page ) {
118118

119119
$this->page = $page;
120120

121-
add_settings_section( self::SID, $label, [ $this, 'add_settings' ], $page );
121+
add_settings_section( self::SID, $label, array( $this, 'add_settings' ), $page );
122122
}
123123

124124
/**
125125
* Callback for add_settings_section in admin_register
126126
*/
127127
public function add_settings(): void {
128-
$args = [ 'msls_admin' => lloc\Msls\MslsAdmin::init() ];
128+
$args = array( 'msls_admin' => lloc\Msls\MslsAdmin::init() );
129129

130130
$label = __( 'Theme Location', 'mslsmenu' );
131-
$callback = [ $this, 'theme_location' ];
131+
$callback = array( $this, 'theme_location' );
132132
add_settings_field( 'mslsmenu_theme_location', $label, $callback, $this->page, self::SID, $args );
133133

134134
$label = __( 'Display', 'mslsmenu' );
135-
$callback = [ $this, 'display' ];
135+
$callback = array( $this, 'display' );
136136
add_settings_field( 'mslsmenu_display', $label, $callback, $this->page, self::SID, $args );
137137

138-
$fields = [
138+
$fields = array(
139139
'mslsmenu_before_output' => __( 'Text/HTML before the list', 'mslsmenu' ),
140140
'mslsmenu_after_output' => __( 'Text/HTML after the list', 'mslsmenu' ),
141141
'mslsmenu_before_item' => __( 'Text/HTML before each item', 'mslsmenu' ),
142142
'mslsmenu_after_item' => __( 'Text/HTML after each item', 'mslsmenu' ),
143-
];
143+
);
144144

145-
$callback = [ $this, 'input' ];
145+
$callback = array( $this, 'input' );
146146
foreach ( $fields as $id => $label ) {
147147
$args['mslsmenu_input'] = $id;
148148
add_settings_field( $id, $label, $callback, $this->page, self::SID, $args );
@@ -157,26 +157,26 @@ public function add_settings(): void {
157157
public function theme_location( array $args ) {
158158
$menu_locations = get_nav_menu_locations();
159159
$theme_locations = $this->options->mslsmenu_theme_location ?? '';
160-
$options = [
161-
sprintf( '<option value="" %s>%s</option>', $this->selected( '', $theme_locations ), esc_html__( '-- empty --', 'mslsmenu' ) )
162-
];
160+
$options = array(
161+
sprintf( '<option value="" %s>%s</option>', $this->selected( '', $theme_locations ), esc_html__( '-- empty --', 'mslsmenu' ) ),
162+
);
163163

164164
foreach ( array_keys( $menu_locations ) as $value ) {
165165
$options[] = sprintf( '<option value="%1$s" %2$s>%1$s</option>', esc_attr( $value ), $this->selected( $value, $theme_locations ) );
166166
}
167167

168168
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
169-
printf( '<select id="%1$s" name="msls[%1$s][]" multiple="multiple">%2$s</select>', 'mslsmenu_theme_location', implode( '', $options ) );
169+
printf( '<select id="%1$s" name="msls[%1$s][]" multiple="multiple">%2$s</select>', 'mslsmenu_theme_location', implode( '', $options ) );
170170
}
171171

172172
/**
173173
* @param string $needle
174-
* @param mixed $locations
174+
* @param mixed $locations
175175
*
176176
* @return string
177177
*/
178178
protected function selected( string $needle, $locations ): string {
179-
return is_array( $locations ) ? selected( true, in_array( $needle, $locations ), false ) : '';
179+
return is_array( $locations ) ? selected( true, in_array( $needle, $locations, true ), false ) : '';
180180
}
181181

182182
/**
@@ -188,9 +188,9 @@ public function display( array $args ) {
188188
$types = lloc\Msls\MslsLink::get_types_description();
189189
$display = $this->options->mslsmenu_display ?? '0';
190190

191-
/**
192-
* Backward compatibility
193-
*/
191+
/**
192+
* Backward compatibility
193+
*/
194194
if ( ! class_exists( lloc\Msls\Component\Input\Select::class ) ) {
195195
// @codeCoverageIgnoreStart
196196

@@ -211,14 +211,14 @@ public function display( array $args ) {
211211
* @param array $args
212212
*/
213213
public function input( array $args ) {
214-
/**
215-
* Backward compatibility
216-
*/
214+
/**
215+
* Backward compatibility
216+
*/
217217
if ( ! class_exists( 'lloc\Msls\Component\Input\Text' ) ) {
218218
// @codeCoverageIgnoreStart
219219

220220
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
221-
echo $args['msls_admin']->render_input( $args['mslsmenu_input'] );
221+
echo $args['msls_admin']->render_input( $args['mslsmenu_input'] );
222222

223223
return;
224224
// @codeCoverageIgnoreEnd
@@ -228,16 +228,18 @@ public function input( array $args ) {
228228
$value = $this->options->$key ?? '';
229229

230230
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
231-
echo ( new lloc\Msls\Component\Input\Text( $key, $value ) )->render();
231+
echo ( new lloc\Msls\Component\Input\Text( $key, $value ) )->render();
232232
}
233-
234233
}
235234

236235
// @codeCoverageIgnoreStart
237236
if ( function_exists( 'add_action' ) ) {
238-
add_action( 'plugins_loaded', function () {
239-
$options = class_exists( lloc\Msls\MslsOptions::class ) ? lloc\Msls\MslsOptions::instance() : null;
240-
MslsMenu::init( $options );
241-
} );
237+
add_action(
238+
'plugins_loaded',
239+
function () {
240+
$options = class_exists( lloc\Msls\MslsOptions::class ) ? lloc\Msls\MslsOptions::instance() : null;
241+
MslsMenu::init( $options );
242+
}
243+
);
242244
}
243-
// @codeCoverageIgnoreEnd
245+
// @codeCoverageIgnoreEnd

bin/githooks/pre-commit

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
echo ""
4+
echo "phpcbf pre commit hook start"
5+
6+
PHP_CS_FIXER="vendor/bin/phpcbf"
7+
HAS_PHP_CS_FIXER=false
8+
9+
# check if php-cs-fixer is installed as a composer dependency
10+
if [ -x "$PHP_CS_FIXER" ]; then
11+
HAS_PHP_CS_FIXER=true
12+
fi
13+
14+
if $HAS_PHP_CS_FIXER; then
15+
16+
# gets a list of all staged but not deleted php-files
17+
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACMRTUXB HEAD | grep '\.php$')
18+
19+
if [ ! -z "${CHANGED_FILES}" ]; then
20+
# runs phpcbf on the changed files
21+
vendor/bin/phpcbf -w ${CHANGED_FILES}
22+
# adds the changed files to staging again
23+
git add ${CHANGED_FILES}
24+
fi
25+
fi
26+
27+
echo "phpcbf pre commit hook finish"
28+
echo ""

composer.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,29 @@
1313
"brain/monkey": "2.*",
1414
"phpstan/phpstan": "^1.8",
1515
"szepeviktor/phpstan-wordpress": "^1.1",
16-
"phpstan/extension-installer": "^1.1"
16+
"phpstan/extension-installer": "^1.1",
17+
"wp-coding-standards/wpcs": "^3.0"
1718
},
1819
"autoload-dev": {
1920
"files": [ "MslsMenu.php" ]
2021
},
2122
"scripts": {
2223
"test": "vendor/bin/pest",
2324
"coverage": "php -d xdebug.mode=coverage vendor/bin/pest --coverage",
24-
"analyze": "vendor/bin/phpstan analyze",
25+
"phpstan": "vendor/bin/phpstan analyze",
2526
"git-release": "bin/git-release.sh",
2627
"build": [
2728
"@git-release"
29+
],
30+
"githooks": [
31+
"if [ -e bin/githooks/pre-commit ]; then cp bin/githooks/pre-commit ./.git/hooks/; fi",
32+
"if [ -e .git/hooks/pre-commit ]; then chmod 0755 .git/hooks/pre-commit; fi"
33+
],
34+
"post-install-cmd": [
35+
"@githooks"
36+
],
37+
"post-update-cmd": [
38+
"@githooks"
2839
]
2940
},
3041
"authors": [
@@ -45,7 +56,8 @@
4556
"allow-plugins": {
4657
"composer/installers": true,
4758
"phpstan/extension-installer": true,
48-
"pestphp/pest-plugin": true
59+
"pestphp/pest-plugin": true,
60+
"dealerdirect/phpcodesniffer-composer-installer": true
4961
}
5062
}
5163
}

languages/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php header( 'location: http://' . filter_input( INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL ) ); ?>
1+
<?php header( 'location: http://' . filter_input( INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL ) );

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Contributors: realloc
44
Donate link: http://www.greenpeace.org/international/
55
Tags: multilingual, multisite, language, switcher, menu
66
Requires at least: 5.3
7-
Tested up to: 6.7
7+
Tested up to: 6.8
88
Requires PHP: 7.4
99
Stable tag: 2.5.1
1010
License: GPLv2 or later

0 commit comments

Comments
 (0)