Skip to content

split up array alignment sniff #54

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
* @version GIT: master
* @link https://github.com/Mayflower/mo4-coding-standard
*/
namespace MO4\Sniffs\Formatting;
namespace MO4\Sniffs\Arrays;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens as PHP_CodeSniffer_Tokens;

/**
* Array Alignment sniff.
* Array Double Arrow Alignment sniff.
*
* '=>' must be aligned in arrays, and the key and the '=>' must be in the same line
*
Expand All @@ -30,7 +30,7 @@
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/Mayflower/mo4-coding-standard
*/
class ArrayAlignmentSniff implements Sniff
class ArrayDoubleArrowAlignmentSniff implements Sniff
{
/**
* Define all types of arrays.
Expand Down Expand Up @@ -80,21 +80,6 @@ public function process(File $phpcsFile, $stackPtr)

if ($tokens[$start]['line'] === $tokens[$end]['line']) {
return;
} if ($tokens[($end - 2)]['line'] === $tokens[$end]['line']) {
if ($current['code'] === T_ARRAY) {
$arrayBrackets = 'parenthesis';
} else {
$arrayBrackets = 'bracket';
}

$phpcsFile->addError(
sprintf(
'closing %s of array must in own line',
$arrayBrackets
),
$end,
'ClosingMustBeInOwnLine'
);
}

$assignments = array();
Expand Down
121 changes: 121 additions & 0 deletions MO4/Sniffs/Arrays/MultiLineArraySniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* This file is part of the mo4-coding-standard (phpcs standard)
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer-MO4
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/Mayflower/mo4-coding-standard
*/
namespace MO4\Sniffs\Arrays;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Multi Line Array sniff.
*
* @category PHP
* @package PHP_CodeSniffer-MO4
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/Mayflower/mo4-coding-standard
*/
class MultiLineArraySniff implements Sniff
{
/**
* Define all types of arrays.
*
* @var array
*/
protected $arrayTokens = array(
T_ARRAY,
T_OPEN_SHORT_ARRAY,
);


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(int)
* @see Tokens.php
*/
public function register()
{
return $this->arrayTokens;

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$current = $tokens[$stackPtr];

if ($current['code'] === T_ARRAY) {
$arrayType = 'parenthesis';
$start = $current['parenthesis_opener'];
$end = $current['parenthesis_closer'];
} else {
$arrayType = 'bracket';
$start = $current['bracket_opener'];
$end = $current['bracket_closer'];
}

if ($tokens[$start]['line'] === $tokens[$end]['line']) {
return;
}

if ($tokens[($start + 2)]['line'] === $tokens[$start]['line']) {
$fixable = $phpcsFile->addFixableError(
sprintf(
'opening %s of multi line array must be followed by newline',
$arrayType
),
$start,
'OpeningMustBeFollowedByNewline'
);

if ($fixable === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewline($start);
$phpcsFile->fixer->endChangeset();
}
}

if ($tokens[($end - 2)]['line'] === $tokens[$end]['line']) {
$fixable = $phpcsFile->addFixableError(
sprintf(
'closing %s of multi line array must in own line',
$arrayType
),
$end,
'ClosingMustBeInOwnLine'
);

if ($fixable === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addNewlineBefore($end);
$phpcsFile->fixer->endChangeset();
}
}

}//end process()


}//end class
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,3 @@ $a = [
98765432
=> 'fail',
];

$values = [
'Test:1' => [
'object_id' => 1,
'object_class' => 'Test',
'object_class_name' => 'TestClass',
'object_name' => 'TestObject',
'id' => 'Test:1', ],
'Test:2' => [
'object_id' => 2,
'object_class' => 'Test',
'object_class_name' => 'TestClass',
'object_name' => 'TestObject',
'id' => 'Test:2', ], ];


$fail = array(
1);
137 changes: 137 additions & 0 deletions MO4/Tests/Arrays/ArrayDoubleArrowAlignmentUnitTest.fail.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php


$a = [
'one' => 1,
'four' => 4,
];

$a = [
'one' => 1,
2,
'four' => 4,
];

// bad
$a = [
'one' => 1, 'two' => 2,
'three' => 1, 'four' => 4,
];

$a = [
'one' => 1,
2,
'four' /* comment */ => 4,
];

$a = [
'one' => 1,
2,
// comment
'four' => 4,
];

$a = [
2,
'four' => 4,
// comment
'one' => /* comment */
1,
];

$a = array(
'short' => 1,
'verylongerkey' => 2,
'x' => 3,
);

$a = [
'short' => 1,
'verylongerkey' => 2,
'x' => 3,
];

$a = array(
'short' => 1,
'verylongerkey' => 2,
'array' => array(
'val1' => 1,
'val112' => 1,
'val11234567' => 1,
'v' => 1,
),
'x' => 3,
);

$a = [
'short' => 1,
'verylongerkey' => 2,
'array' => [
'val1' => 1,
'val112' => 1,
'val1123456' => 1,
'v' => 1,
],
'x' => 3,
];

$a = [
'short' => 1,
'verylongerkey' => 2,
'array' => array(
'val1' => 1,
'val112' => 1,
'val11234567' => 1,
'v' => 1,
),
'x' => 3,
];

$a = array(
'short' => 1,
'verylongerkey' => 2,
'array' => [
'val1' => 1,
'val112' => 1,
'val1123' => 1,
'v' => 1,
],
'x' => 3,
);

$a = [
0 => [ 1 => 'O', 0 => 'O'],
1 => [ 1 => 'O', 0 => 'I'],
2 => [ 1 => 'I', 0 => 'O'],
3 => [ 1 => 'I', 0 => 'I'],
];

foreach ($x as $k => $v) {

}

$a = [
function () {
$b = [
'one' => 1,
'four' => 4,
];
foreach ($b as $k => $v) {
}
$bar = [
'one' => 1,
'four' => 4,
];
foreach ($bar as $key => $value) {
}
},
function () {
},
'one' => 1,
2,
'four' => 2,
123456789
/* bla */ => 'fail',
98765432
=> 'fail',
];
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ $a = [
'one' => 1,
2,
'four' => 2,
];
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
* @link https://github.com/Mayflower/mo4-coding-standard
*/

namespace MO4\Tests\Arrays;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the ArrayValueAlignment sniff.
* Unit test class for @see ArrayDoubleArrowAlignmentSniff
*
* A sniff unit test checks a .inc file for expected violations of a single
* coding standard. Expected errors and warnings are stored in this class.
*
* @category PHP
* @package PHP_CodeSniffer-MO4
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/Mayflower/mo4-coding-standard
*/
namespace MO4\Tests\Formatting;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ArrayAlignmentUnitTest extends AbstractSniffUnitTest
class ArrayDoubleArrowAlignmentUnitTest extends AbstractSniffUnitTest
{


Expand All @@ -49,9 +49,9 @@ class ArrayAlignmentUnitTest extends AbstractSniffUnitTest
protected function getErrorList($testFile='')
{
switch ($testFile) {
case 'ArrayAlignmentUnitTest.pass.inc':
case 'ArrayDoubleArrowAlignmentUnitTest.pass.inc':
return array();
case 'ArrayAlignmentUnitTest.fail.inc':
case 'ArrayDoubleArrowAlignmentUnitTest.fail.inc':
return array(
5 => 1,
10 => 1,
Expand Down Expand Up @@ -82,9 +82,6 @@ protected function getErrorList($testFile='')
132 => 1,
134 => 1,
136 => 2,
145 => 1,
151 => 2,
155 => 1,
);
}//end switch

Expand Down
Loading