Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit 9f2ba55

Browse files
committed
Merge branch 'release/1.0.0-beta0010'
2 parents ec22dd4 + eebe706 commit 9f2ba55

File tree

8 files changed

+102
-157
lines changed

8 files changed

+102
-157
lines changed

CodeIgniter4/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 39 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -294,103 +294,6 @@ public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arra
294294
}//end processSingleLineArray()
295295

296296

297-
/**
298-
* Get the stackPtr where the array declaration starts.
299-
*
300-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
301-
* @param int $stackPtr The position of the current token
302-
* in the stack passed in $tokens.
303-
*
304-
* @todo This is 'brute force' at the moment and ought to be refactored.
305-
*
306-
* @return int|false
307-
*/
308-
public function arrayDeclaredAt($phpcsFile, $stackPtr)
309-
{
310-
$tokens = $phpcsFile->getTokens();
311-
312-
// Possible indent starting token.
313-
$indentStart = $stackPtr;
314-
315-
// Tokens that could come before an array declaration variable.
316-
$preTokens = array(
317-
T_VAR => true,
318-
T_PUBLIC => true,
319-
T_PRIVATE => true,
320-
T_PROTECTED => true,
321-
T_ARRAY_CAST => true,
322-
T_UNSET_CAST => true,
323-
T_OBJECT_CAST => true,
324-
T_STATIC => true,
325-
);
326-
327-
$before1 = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
328-
329-
// Is it a variable.
330-
// - '$arr = [];'.
331-
if ($tokens[$before1]['code'] === T_VARIABLE) {
332-
// Store this in case this is the start.
333-
$indentStart = $before1;
334-
335-
// Does it have visibility scope or a type hint?
336-
// - 'private $arr = [];'
337-
// - '(array) $arr = [];'.
338-
// - 'private static $arr = [];'.
339-
$before2 = $phpcsFile->findPrevious(T_WHITESPACE, ($before1 - 1), null, true);
340-
if ($before2 !== false && isset($preTokens[$tokens[$before2]['code']]) === true) {
341-
// It is preceded with scope or type.
342-
$indentStart = $before2;
343-
344-
// Could still need to go back one level if it's static.
345-
// - 'private static $arr = [];'.
346-
$before3 = $phpcsFile->findPrevious(T_WHITESPACE, ($before2 - 1), null, true);
347-
if ($before3 !== false && isset($preTokens[$tokens[$before3]['code']]) === true) {
348-
// It is preceded with scope or type.
349-
$indentStart = $before3;
350-
}
351-
}
352-
}//end if
353-
354-
// - $obj->arr[] = [];
355-
if ($tokens[$before1]['code'] === T_CLOSE_SQUARE_BRACKET) {
356-
$before1 = $phpcsFile->findPrevious(T_WHITESPACE, ($tokens[$before1]['bracket_opener'] - 1), null, true);
357-
}
358-
359-
// Is it a string?, if so expect object or constant.
360-
// - '$obj->arr = [];'.
361-
// - 'MY_CONST = [];'
362-
// - 'const MY_CONST = [];.
363-
if ($tokens[$before1]['code'] === T_STRING) {
364-
$before2 = $phpcsFile->findPrevious(T_WHITESPACE, ($before1 - 1), null, true);
365-
// Is it a constant?
366-
if ($tokens[$before2]['code'] === T_CONST) {
367-
$indentStart = $before2;
368-
return $indentStart;
369-
}
370-
371-
// Is it an object?
372-
if ($tokens[$before2]['code'] === T_OBJECT_OPERATOR) {
373-
$indentStart = $before2;
374-
375-
$before3 = $phpcsFile->findPrevious(T_WHITESPACE, ($before2 - 1), null, true);
376-
if ($tokens[$before3]['code'] === T_VARIABLE) {
377-
$indentStart = $before3;
378-
379-
// Does it have visibility scope or a is it cast?
380-
$before4 = $phpcsFile->findPrevious(T_WHITESPACE, ($before3 - 1), null, true);
381-
if ($before4 !== false && isset($preTokens[$tokens[$before4]['code']]) === true) {
382-
// It is preceded with scope or type.
383-
$indentStart = $before4;
384-
}
385-
}
386-
}
387-
}//end if
388-
389-
return $indentStart;
390-
391-
}//end arrayDeclaredAt()
392-
393-
394297
/**
395298
* Processes a multi-line array definition.
396299
*
@@ -408,25 +311,51 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
408311
$keywordStart = $tokens[$stackPtr]['column'];
409312

410313
$prevNonWhitespaceToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
411-
if ($tokens[$prevNonWhitespaceToken]['code'] === T_EQUAL
412-
|| $tokens[$prevNonWhitespaceToken]['code'] === T_OPEN_PARENTHESIS
413-
|| $tokens[$prevNonWhitespaceToken]['code'] === T_RETURN
414-
) {
314+
315+
// Find where this array should be indented from.
316+
switch ($tokens[$prevNonWhitespaceToken]['code']) {
317+
case T_EQUAL:
318+
case T_OPEN_PARENTHESIS:
415319
// It's "=", "(" or "return".
416-
$indentStart = $this->arrayDeclaredAt($phpcsFile, $prevNonWhitespaceToken);
417-
} else if ($tokens[$prevNonWhitespaceToken]['code'] === T_DOUBLE_ARROW) {
320+
$starts = array(
321+
T_VARIABLE,
322+
T_VAR,
323+
T_PUBLIC,
324+
T_PRIVATE,
325+
T_PROTECTED,
326+
T_ARRAY_CAST,
327+
T_UNSET_CAST,
328+
T_OBJECT_CAST,
329+
T_STATIC,
330+
T_CONST,
331+
);
332+
333+
$firstOnLine = $phpcsFile->findFirstOnLine($starts, $prevNonWhitespaceToken);
334+
$indentStart = $firstOnLine;
335+
break;
336+
case T_DOUBLE_ARROW:
418337
// It's an array in an array "=> []".
419338
$indentStart = $phpcsFile->findPrevious(T_WHITESPACE, ($prevNonWhitespaceToken - 1), null, true);
420-
} else if ($tokens[$prevNonWhitespaceToken]['code'] === T_OPEN_SHORT_ARRAY
421-
|| $tokens[$prevNonWhitespaceToken]['code'] === T_COMMA
422-
) {
339+
break;
340+
case T_RETURN:
341+
$indentStart = $prevNonWhitespaceToken;
342+
break;
343+
case T_COMMENT:
344+
case T_OPEN_SHORT_ARRAY:
423345
// It's an array in an array "[[]]" or the end of an array line "[],".
424346
$indentStart = $stackPtr;
425-
} else {
426-
// Nothing expected preceded this so return here and
347+
break;
348+
case T_COMMA:
349+
// The end of an array line "[],".
350+
// Argument in a function "$item->save($data, [...], ...)".
351+
$firstOnLine = $phpcsFile->findFirstOnLine(array(T_VARIABLE, T_CLOSE_SHORT_ARRAY), $prevNonWhitespaceToken);
352+
$indentStart = $firstOnLine;
353+
break;
354+
default:
355+
// Nothing expected preceded this so leave ptr where it is and
427356
// it should get picked in a future pass.
428-
return;
429-
}
357+
$indentStart = $stackPtr;
358+
}//end switch
430359

431360
// Check the closing bracket is on a new line.
432361
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), $arrayStart, true);

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ class MyClass {
9393
'b'=> 2,
9494
'c' => 3,
9595
];
96+
97+
$cache->save($cacheKeyPagination, [
98+
'page' => $this->pager->getCurrentPage(),
99+
'perPage' => $this->pager->getPerPage(),
100+
'total' => $this->pager->getPerPage() * $this->pager->getPageCount(),
101+
] , 15);
96102
}

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,10 @@ class MyClass {
160160
'b' => 2,
161161
'c' => 3,
162162
];
163+
164+
$cache->save($cacheKeyPagination, [
165+
'page' => $this->pager->getCurrentPage(),
166+
'perPage' => $this->pager->getPerPage(),
167+
'total' => $this->pager->getPerPage() * $this->pager->getPageCount(),
168+
] , 15);
163169
}

CodeIgniter4/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,50 @@ public function setCliValues($testFile, $config)
4242
public function getErrorList()
4343
{
4444
return array(
45-
6 => 1,
46-
7 => 1,
47-
8 => 1,
48-
9 => 1,
49-
10 => 1,
50-
11 => 1,
51-
12 => 1,
52-
13 => 1,
53-
14 => 2,
54-
15 => 1,
55-
16 => 1,
56-
22 => 1,
57-
29 => 1,
58-
30 => 1,
59-
36 => 1,
60-
37 => 2,
61-
42 => 2,
62-
43 => 2,
63-
44 => 2,
64-
45 => 2,
65-
47 => 2,
66-
48 => 2,
67-
49 => 2,
68-
52 => 1,
69-
56 => 1,
70-
60 => 1,
71-
61 => 1,
72-
62 => 1,
73-
64 => 1,
74-
70 => 1,
75-
71 => 1,
76-
77 => 1,
77-
80 => 1,
78-
81 => 1,
79-
82 => 1,
80-
85 => 1,
81-
87 => 1,
82-
89 => 1,
83-
92 => 1,
84-
93 => 1,
45+
6 => 1,
46+
7 => 1,
47+
8 => 1,
48+
9 => 1,
49+
10 => 1,
50+
11 => 1,
51+
12 => 1,
52+
13 => 1,
53+
14 => 2,
54+
15 => 1,
55+
16 => 1,
56+
22 => 1,
57+
29 => 1,
58+
30 => 1,
59+
36 => 1,
60+
37 => 2,
61+
42 => 2,
62+
43 => 2,
63+
44 => 2,
64+
45 => 2,
65+
47 => 2,
66+
48 => 2,
67+
49 => 2,
68+
52 => 1,
69+
56 => 1,
70+
60 => 1,
71+
61 => 1,
72+
62 => 1,
73+
64 => 1,
74+
70 => 1,
75+
71 => 1,
76+
77 => 1,
77+
80 => 1,
78+
81 => 1,
79+
82 => 1,
80+
85 => 1,
81+
87 => 1,
82+
89 => 1,
83+
92 => 1,
84+
93 => 1,
85+
98 => 1,
86+
99 => 1,
87+
100 => 1,
88+
101 => 1,
8589
);
8690

8791
}//end getErrorList()

CodeIgniter4/ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Louis Linehan <[email protected]>
88
* @copyright 2017 Louis Linehan
99
* @license https://github.com/louisl/CodeIgniter4-Standard/blob/master/LICENSE MIT License
10-
* @version Version 1.0.0-beta0009
10+
* @version Version 1.0.0-beta0010
1111
-->
1212
<!--
1313
Files MUST have a doc block comment.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
[CodeIgniter](https://codeigniter.com) 4 coding standard for use with [PHP_CodeSniffer 3](https://github.com/squizlabs/PHP_CodeSniffer).
44

5-
Version 1.0.0-beta0009
5+
Version 1.0.0-beta0010
66

77
| Master | Develop |
88
| :---: | :---: |
99
| [![Build Status](https://travis-ci.org/louisl/CodeIgniter4-Standard.svg?branch=master)](https://travis-ci.org/louisl/CodeIgniter4-Standard) | [![Build Status](https://travis-ci.org/louisl/CodeIgniter4-Standard.svg?branch=develop)](https://travis-ci.org/louisl/CodeIgniter4-Standard) |
10-
| [![Coverage Status](https://coveralls.io/repos/github/louisl/CodeIgniter4-Standard/badge.svg?branch=master)](https://coveralls.io/github/louisl/CodeIgniter4-Standard?branch=master) | [![Coverage Status](https://coveralls.io/repos/github/louisl/CodeIgniter4-Standard/badge.svg?branch=develop)](https://coveralls.io/github/louisl/CodeIgniter4-Standard?branch=develop)
10+
| [![Coverage Status](https://coveralls.io/repos/github/louisl/CodeIgniter4-Standard/badge.svg?branch=master)](https://coveralls.io/github/louisl/CodeIgniter4-Standard?branch=master) | [![Coverage Status](https://coveralls.io/repos/github/louisl/CodeIgniter4-Standard/badge.svg?branch=develop)](https://coveralls.io/github/louisl/CodeIgniter4-Standard?branch=develop) |
1111

1212
***This is currently a work in progress.***
1313

@@ -34,7 +34,7 @@ Set the `phpcs standard path` and `phpcbf standard path` in your editor/plugin c
3434

3535
### Download install
3636

37-
Download [CodeIgniter4-Standard](https://github.com/louisl/CodeIgniter4-Standard/archive/v1.0.0-beta0009.zip).
37+
Download [CodeIgniter4-Standard](https://github.com/louisl/CodeIgniter4-Standard/archive/v1.0.0-beta0010.zip).
3838

3939
Set `standard ` paths to your local filesystem:
4040

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "louisl/codeigniter4-standard",
33
"description": "CodeIgniter 4 Standard for PHP_CodeSniffer 3.",
4-
"version":"1.0.0-beta0009",
4+
"version":"1.0.0-beta0010",
55
"license":"MIT",
66
"authors": [
77
{

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)