Skip to content

Commit 7da9b37

Browse files
author
Igor Melnikov
committed
MAGETWO-61240: Fix \Magento\Sniffs\Translation\ConstantUsageSniff
Fixing static test
1 parent 28bd1a1 commit 7da9b37

File tree

4 files changed

+164
-93
lines changed

4 files changed

+164
-93
lines changed

dev/tests/static/framework/Magento/Sniffs/Translation/ConstantUsageSniff.php

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66
namespace Magento\Sniffs\Translation;
77

8-
use PHP_CodeSniffer_File;
9-
108
/**
119
* Make sure that constants are not used as the first argument of translation function.
1210
*/
13-
class ConstantUsageSniff extends \Generic_Sniffs_Files_LineLengthSniff
11+
class ConstantUsageSniff implements \PHP_CodeSniffer_Sniff
1412
{
1513
/**
1614
* Having previous line content allows to process multi-line declaration.
@@ -20,24 +18,76 @@ class ConstantUsageSniff extends \Generic_Sniffs_Files_LineLengthSniff
2018
protected $previousLineContent = '';
2119

2220
/**
23-
* {@inheritdoc}
21+
* {@inheritDoc}
22+
*/
23+
public function register()
24+
{
25+
return [T_OPEN_TAG];
26+
27+
}
28+
29+
/**
30+
* Copied from \Generic_Sniffs_Files_LineLengthSniff
31+
*
32+
* {@inheritDoc}
2433
*/
25-
protected function checkLineLength(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent)
34+
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
2635
{
27-
$previousLineRegexp = '~__\($|Phrase\($~';
28-
$currentLineRegexp = '~__\(.+\)|Phrase\(.+\)~';
36+
$tokens = $phpcsFile->getTokens();
37+
38+
// Make sure this is the first open tag.
39+
$previousOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
40+
if ($previousOpenTag !== false) {
41+
return;
42+
}
43+
44+
$tokenCount = 0;
45+
$currentLineContent = '';
46+
$currentLine = 1;
47+
48+
$trim = (strlen($phpcsFile->eolChar) * -1);
49+
for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
50+
if ($tokens[$tokenCount]['line'] === $currentLine) {
51+
$currentLineContent .= $tokens[$tokenCount]['content'];
52+
} else {
53+
$currentLineContent = substr($currentLineContent, 0, $trim);
54+
$this->checkIfFirstArgumentConstant($phpcsFile, ($tokenCount - 1), $currentLineContent);
55+
$currentLineContent = $tokens[$tokenCount]['content'];
56+
$currentLine++;
57+
}
58+
}
59+
60+
$currentLineContent = substr($currentLineContent, 0, $trim);
61+
$this->checkIfFirstArgumentConstant($phpcsFile, ($tokenCount - 1), $currentLineContent);
62+
}
63+
64+
/**
65+
* Checks if first argument of \Magento\Framework\Phrase or translation function is a constant
66+
*
67+
* @param \PHP_CodeSniffer_File $phpcsFile
68+
* @param int $stackPtr
69+
* @param string $lineContent
70+
* @return void
71+
*/
72+
private function checkIfFirstArgumentConstant(
73+
\PHP_CodeSniffer_File $phpcsFile,
74+
$stackPtr,
75+
$lineContent
76+
) {
77+
$previousLineRegexp = '/(__|Phrase)\($/im';
78+
$currentLineRegexp = '/(__|Phrase)\(.+\)/';
2979
$currentLineMatch = preg_match($currentLineRegexp, $lineContent) !== 0;
3080
$previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0;
3181
$this->previousLineContent = $lineContent;
3282
$error = 'Constants are not allowed as the first argument of translation function, use string literal instead';
3383
$constantRegexp = '[^\$\'"]+::[A-Z_0-9]+.*';
3484
if ($currentLineMatch) {
35-
$variableRegexp = "~__\({$constantRegexp}\)|Phrase\({$constantRegexp}\)~";
85+
$variableRegexp = "/(__|Phrase)\({$constantRegexp}\)/";
3686
if (preg_match($variableRegexp, $lineContent) !== 0) {
3787
$phpcsFile->addError($error, $stackPtr, 'VariableTranslation');
3888
}
3989
} else if ($previousLineMatch) {
40-
$variableRegexp = "~^\s+{$constantRegexp}~";
90+
$variableRegexp = "/^{$constantRegexp}/";
4191
if (preg_match($variableRegexp, $lineContent) !== 0) {
4292
$phpcsFile->addError($error, $stackPtr, 'VariableTranslation');
4393
}

dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php

Lines changed: 49 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,109 +24,74 @@ protected function setUp()
2424
}
2525

2626
/**
27-
* @param string $line
28-
* @dataProvider checkLineLengthCorrectArguments
27+
* @param string $file
28+
* @param int $numIncorrectUsages
29+
* @dataProvider processDataProvider
2930
*/
30-
public function testCheckLineLengthCorrectArguments($line)
31+
public function testProcessIncorrectArguments($file, $numIncorrectUsages)
3132
{
32-
$this->fileMock->expects($this->never())
33-
->method('addError');
34-
$this->checkLineLength(10, $line);
35-
}
36-
37-
/**
38-
* @return array
39-
*/
40-
public function checkLineLengthCorrectArguments()
41-
{
42-
return [
43-
[
44-
'__($item)'
45-
],
46-
[
47-
'__($item[ConfigConverter::KEY_TITLE])'
48-
],
49-
[
50-
'__($item[\'value\'])'
51-
],
52-
[
53-
'__($item->getValue())'
54-
],
55-
[
56-
'Phrase($item)'
57-
],
58-
[
59-
'Phrase($item[ConfigConverter::KEY_TITLE])'
60-
],
61-
[
62-
'Phrase($item[\'value\'])'
63-
],
64-
[
65-
'Phrase($item->getValue())'
66-
],
67-
[
68-
'\Magento\Framework\Phrase($item)'
69-
]
70-
];
71-
}
72-
73-
/**
74-
* @param string $line
75-
* @dataProvider checkLineLengthIncorrectArguments
76-
*/
77-
public function testCheckLineLengthIncorrectArguments($line)
78-
{
79-
$lineNumber = 10;
33+
$stackPtr = 10;
34+
$fileContent = file_get_contents(__DIR__ . '/_files/' . $file);
35+
$tokens = $this->tokenizeString($fileContent);
8036
$this->fileMock->expects($this->once())
37+
->method('findPrevious')
38+
->with(
39+
T_OPEN_TAG,
40+
$stackPtr - 1
41+
)
42+
->willReturn(false);
43+
$this->fileMock->expects($this->once())
44+
->method('getTokens')
45+
->willReturn($tokens);
46+
$this->fileMock->eolChar = 2;
47+
$this->fileMock->numTokens = count($tokens);
48+
$this->fileMock->expects($this->exactly($numIncorrectUsages))
8149
->method('addError')
8250
->with(
8351
'Constants are not allowed as the first argument of translation function, use string literal instead',
84-
$lineNumber,
52+
$this->anything(),
8553
'VariableTranslation'
8654
);
87-
$this->checkLineLength($lineNumber, $line);
55+
$this->constantUsageSniff->process($this->fileMock, $stackPtr);
56+
}
57+
58+
/**
59+
* Get tokens for a string
60+
*
61+
* @param string $fileContent
62+
* @return array
63+
*/
64+
private function tokenizeString($fileContent)
65+
{
66+
$lineNumber = 1;
67+
$tokens = token_get_all($fileContent);
68+
$snifferTokens = [];
69+
for ($i = 0; $i < count($tokens); $i++) {
70+
$content = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
71+
$snifferTokens[$i]['line'] = $lineNumber;
72+
$snifferTokens[$i]['content'] = $content;
73+
$trimmedContent = trim($content, ' ');
74+
if ($trimmedContent == PHP_EOL || $trimmedContent == PHP_EOL . PHP_EOL) {
75+
$lineNumber++;
76+
}
77+
}
78+
return $snifferTokens;
8879
}
8980

9081
/**
9182
* @return array
9283
*/
93-
public function checkLineLengthIncorrectArguments()
84+
public function processDataProvider()
9485
{
9586
return [
9687
[
97-
'$item[ConfigConverter::KEY_TITLE] = __(Converter::KEY_TITLE)'
98-
],
99-
[
100-
'$item[ConfigConverter::KEY_TITLE] = __(self::KEY_TITLE)'
88+
'incorrect_arguments.txt',
89+
9
10190
],
10291
[
103-
'$item[ConfigConverter::KEY_TITLE] = __(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
104-
],
105-
[
106-
'Phrase(Converter::KEY_TITLE)'
107-
],
108-
[
109-
'Phrase(self::KEY_TITLE)'
110-
],
111-
[
112-
'Phrase(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)'
113-
],
114-
[
115-
'\Magento\Framework\Phrase(Converter::KEY_TITLE)'
92+
'correct_arguments.txt',
93+
0
11694
]
11795
];
11896
}
119-
120-
/**
121-
* Call checkLineLength method
122-
*
123-
* @param int $lineNumber
124-
* @param string $line
125-
*/
126-
private function checkLineLength($lineNumber, $line)
127-
{
128-
$reflectionMethod = new \ReflectionMethod(ConstantUsageSniff::class, 'checkLineLength');
129-
$reflectionMethod->setAccessible(true);
130-
$reflectionMethod->invoke($this->constantUsageSniff, $this->fileMock, $lineNumber, $line);
131-
}
13297
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$item[ConfigConverter::KEY_TITLE] = __($item)
8+
9+
$item[ConfigConverter::KEY_TITLE] = __($item[Converter::KEY_TITLE])
10+
11+
$item[ConfigConverter::KEY_TITLE] = __($item['\Magento\Support\Model\Report\Config\Converter::KEY_TITLE'])
12+
13+
$item[ConfigConverter::KEY_TITLE] = __($item['value'])
14+
15+
$item[ConfigConverter::KEY_TITLE] = __(
16+
$item
17+
)
18+
19+
Phrase($item)
20+
21+
Phrase($item[Converter::KEY_TITLE])
22+
23+
Phrase($item['\Magento\Support\Model\Report\Config\Converter::KEY_TITLE'])
24+
25+
\Magento\Framework\Phrase($item['value'])
26+
27+
\Magento\Framework\Phrase(
28+
$item[Converter::KEY_TITLE]
29+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$item[ConfigConverter::KEY_TITLE] = __(Converter::KEY_TITLE)
8+
9+
$item[ConfigConverter::KEY_TITLE] = __(self::KEY_TITLE)
10+
11+
$item[ConfigConverter::KEY_TITLE] = __(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)
12+
13+
$item[ConfigConverter::KEY_TITLE] = __(
14+
Converter::KEY_TITLE
15+
)
16+
17+
Phrase(Converter::KEY_TITLE)
18+
19+
Phrase(self::KEY_TITLE)
20+
21+
Phrase(\Magento\Support\Model\Report\Config\Converter::KEY_TITLE)
22+
23+
\Magento\Framework\Phrase(Converter::KEY_TITLE)
24+
25+
\Magento\Framework\Phrase(
26+
Converter::KEY_TITLE
27+
)

0 commit comments

Comments
 (0)