Skip to content
Snippets Groups Projects
Commit 1cd4dc84 authored by Igor Melnikov's avatar Igor Melnikov
Browse files

MAGETWO-61240: Fix \Magento\Sniffs\Translation\ConstantUsageSniff

Addressing code review feedback
parent 611ad9d1
No related merge requests found
...@@ -13,19 +13,6 @@ use Magento\TestFramework\Utility\File\RegexIteratorFactory; ...@@ -13,19 +13,6 @@ use Magento\TestFramework\Utility\File\RegexIteratorFactory;
*/ */
class File class File
{ {
/**@#+
* File types offset flags
*/
const INCLUDE_APP_CODE = Files::INCLUDE_APP_CODE;
const INCLUDE_PUB_CODE = Files::INCLUDE_PUB_CODE;
const INCLUDE_LIBS = Files::INCLUDE_LIBS;
const INCLUDE_TEMPLATES = Files::INCLUDE_TEMPLATES;
const INCLUDE_TESTS = Files::INCLUDE_TESTS;
const INCLUDE_SETUP = 128;
const INCLUDE_NON_CLASSES = Files::INCLUDE_NON_CLASSES;
const AS_DATA_SET = Files::AS_DATA_SET;
/**#@-*/
/** /**
* @var RegexIteratorFactory * @var RegexIteratorFactory
*/ */
...@@ -53,29 +40,23 @@ class File ...@@ -53,29 +40,23 @@ class File
/** /**
* Get list of PHP files * Get list of PHP files
* *
* @param int $flags
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public function getPhpFiles( public function getPhpFiles()
$flags = self::INCLUDE_APP_CODE {
| self::INCLUDE_PUB_CODE
| self::INCLUDE_LIBS
| self::INCLUDE_TEMPLATES
| self::INCLUDE_TESTS
| self::INCLUDE_SETUP
| self::INCLUDE_NON_CLASSES
| self::AS_DATA_SET
) {
$files = array_merge( $files = array_merge(
$this->fileUtilities->getPhpFiles((2147483647 - self::AS_DATA_SET) & $flags), $this->fileUtilities->getPhpFiles(
$this->getSetupPhpFiles($flags) Files::INCLUDE_APP_CODE
| Files::INCLUDE_PUB_CODE
| Files::INCLUDE_LIBS
| Files::INCLUDE_TEMPLATES
| Files::INCLUDE_TESTS
| Files::INCLUDE_NON_CLASSES
),
$this->getSetupPhpFiles()
); );
return Files::composeDataSets($files);
if ($flags & self::AS_DATA_SET) {
return Files::composeDataSets($files);
}
return $files;
} }
/** /**
...@@ -84,17 +65,15 @@ class File ...@@ -84,17 +65,15 @@ class File
* @param int $flags * @param int $flags
* @return array * @return array
*/ */
private function getSetupPhpFiles($flags) private function getSetupPhpFiles()
{ {
$files = []; $files = [];
if ($flags & self::INCLUDE_SETUP) { $regexIterator = $this->regexIteratorFactory->create(
$regexIterator = $this->regexIteratorFactory->create( BP . '/setup',
BP . '/setup', '/.*php^/'
'/.*php^/' );
); foreach ($regexIterator as $file) {
foreach ($regexIterator as $file) { $files = array_merge($files, [$file]);
$files = array_merge($files, [$file]);
}
} }
return $files; return $files;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
namespace Magento\TestFramework\Utility; namespace Magento\TestFramework\Utility;
/** /**
* A helper detects functions * Check if one or more functions are used in the file
*/ */
class FunctionDetector class FunctionDetector
{ {
...@@ -27,16 +27,16 @@ class FunctionDetector ...@@ -27,16 +27,16 @@ class FunctionDetector
* ], * ],
* ] * ]
* *
* @param string $fileFullPath * @param string $filePath
* @param string[] $functions * @param string[] $functions
* @return array * @return array
*/ */
public function detectFunctions($fileFullPath, $functions) public function detect($filePath, $functions)
{ {
$result = []; $result = [];
$regexp = $this->composeRegexp($functions); $regexp = $this->composeRegexp($functions);
if ($regexp) { if ($regexp) {
$file = file($fileFullPath); $file = file($filePath);
array_unshift($file, ''); array_unshift($file, '');
$lines = preg_grep( $lines = preg_grep(
$regexp, $regexp,
......
...@@ -46,54 +46,20 @@ class FileTest extends \PHPUnit_Framework_TestCase ...@@ -46,54 +46,20 @@ class FileTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testGetPhpFilesWithoutSetup() public function testGetPhpFiles()
{ {
$appFiles = [ $appFiles = [
'file1', 'file1',
'file2' 'file2'
]; ];
$setupFiles = [
'file3'
];
$expected = [ $expected = [
'file1' => ['file1'], 'file1' => ['file1'],
'file2' => ['file2'] 'file2' => ['file2'],
'file3' => ['file3']
]; ];
$this->regexIteratorFactoryMock->expects($this->never())
->method('create');
$this->fileUtilitiesMock->expects($this->once())
->method('getPhpFiles')
->with(
File::INCLUDE_APP_CODE
| File::INCLUDE_PUB_CODE
| File::INCLUDE_LIBS
| File::INCLUDE_TEMPLATES
| File::INCLUDE_TESTS
| File::INCLUDE_NON_CLASSES
)
->willReturn($appFiles);
$actual = $this->file->getPhpFiles(
File::INCLUDE_APP_CODE
| File::INCLUDE_PUB_CODE
| File::INCLUDE_LIBS
| File::INCLUDE_TEMPLATES
| File::INCLUDE_TESTS
| File::INCLUDE_NON_CLASSES
| File::AS_DATA_SET
);
$this->assertEquals($expected, $actual);
}
/**
* @param array $appFiles
* @param array$setupFiles
* @param int $flags
* @param array $expected
* @dataProvider getPhpFilesWithSetupDataProvider
*/
public function testGetPhpFilesWithSetup(
$appFiles,
$setupFiles,
$flags,
$expected
) {
$iteratorMock = $this->getMock(\IteratorAggregate::class, [], [], '', false); $iteratorMock = $this->getMock(\IteratorAggregate::class, [], [], '', false);
$iteratorMock->expects($this->any()) $iteratorMock->expects($this->any())
->method('getIterator') ->method('getIterator')
...@@ -104,61 +70,14 @@ class FileTest extends \PHPUnit_Framework_TestCase ...@@ -104,61 +70,14 @@ class FileTest extends \PHPUnit_Framework_TestCase
$this->fileUtilitiesMock->expects($this->once()) $this->fileUtilitiesMock->expects($this->once())
->method('getPhpFiles') ->method('getPhpFiles')
->with( ->with(
File::INCLUDE_APP_CODE Files::INCLUDE_APP_CODE
| File::INCLUDE_PUB_CODE | Files::INCLUDE_PUB_CODE
| File::INCLUDE_LIBS | Files::INCLUDE_LIBS
| File::INCLUDE_TEMPLATES | Files::INCLUDE_TEMPLATES
| File::INCLUDE_TESTS | Files::INCLUDE_TESTS
| File::INCLUDE_SETUP | Files::INCLUDE_NON_CLASSES
| File::INCLUDE_NON_CLASSES
) )
->willReturn($appFiles); ->willReturn($appFiles);
$this->assertEquals($expected, $this->file->getPhpFiles($flags)); $this->assertEquals($expected, $this->file->getPhpFiles());
}
/**
* @return array
*/
public function getPhpFilesWithSetupDataProvider()
{
$flags = File::INCLUDE_APP_CODE
| File::INCLUDE_PUB_CODE
| File::INCLUDE_LIBS
| File::INCLUDE_TEMPLATES
| File::INCLUDE_TESTS
| File::INCLUDE_SETUP
| File::INCLUDE_NON_CLASSES;
return [
[
[
'file1',
'file2'
],
[
'file3'
],
$flags | File::AS_DATA_SET,
[
'file1' => ['file1'],
'file2' => ['file2'],
'file3' => ['file3']
]
],
[
[
'file1',
'file2'
],
[
'file3'
],
$flags,
[
'file1',
'file2',
'file3'
]
]
];
} }
} }
...@@ -11,12 +11,12 @@ class FunctionDetectorTest extends \PHPUnit_Framework_TestCase ...@@ -11,12 +11,12 @@ class FunctionDetectorTest extends \PHPUnit_Framework_TestCase
{ {
$fixturePath = __DIR__ . '/_files/test.txt'; $fixturePath = __DIR__ . '/_files/test.txt';
$expectedResults = [ $expectedResults = [
24 => ['strtoupper', 'md5'], 1 => ['strtoupper', 'strtolower'],
36 => ['security'], 3 => ['foo'],
37 => ['security'], 4 => ['foo'],
]; ];
$functionDetector = new FunctionDetector(); $functionDetector = new FunctionDetector();
$lines = $functionDetector->detectFunctions($fixturePath, ['security', 'md5', 'test', 'strtoupper']); $lines = $functionDetector->detect($fixturePath, ['foo', 'strtoupper', 'test', 'strtolower']);
$this->assertEquals($expectedResults, $lines); $this->assertEquals($expectedResults, $lines);
} }
} }
<?php strtoupper(strtolower($foo . $bar))
/** function foo($merchantMd5, $merchantApiLogin)
* Copyright © 2016 Magento. All rights reserved. $this->generateHash(foo($bar), $foo)
* See COPYING.txt for license details. foo(
*/ 'bar'
namespace Magento\Authorizenet\Model\Security; )
Foo::bar($foo, $this->getData('bar'))
/** $this->foo('bar')
* security Foo::foo()
*/
class Test extends AuthorizenetResponse
{
/**
* Generates an Md5 hash to compare against AuthNet's.
*
* @param string $merchantMd5
* @param string $merchantApiLogin
* @param string $amount
* @param string $transactionId
* @return string
*/
public function checkSecurity($merchantMd5, $merchantApiLogin, $amount, $transactionId)
{
return strtoupper(md5($merchantMd5 . $merchantApiLogin . $transactionId . $amount));
}
/**
* Return if is valid order id.
*
* @param string $merchantMd5
* @param string $merchantApiLogin
* @return bool
*/
public function security($merchantMd5, $merchantApiLogin)
{
$hash = $this->generateHash(security($merchantMd5), $merchantApiLogin, $this->getXAmount());
security(
'something'
);
return Security::compareStrings($hash, $this->getData('x_MD5_Hash'));
$this->security('check');
Security::security();
return $this->getXResponseCode() == \Magento\Authorizenet\Model\Security::RESPONSE_CODE_APPROVED;
}
}
...@@ -97,7 +97,7 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase ...@@ -97,7 +97,7 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase
$invoker( $invoker(
function ($fileFullPath) use ($functionDetector) { function ($fileFullPath) use ($functionDetector) {
$functions = $this->getFunctions($fileFullPath); $functions = $this->getFunctions($fileFullPath);
$lines = $functionDetector->detectFunctions($fileFullPath, array_keys($functions)); $lines = $functionDetector->detect($fileFullPath, array_keys($functions));
$message = ''; $message = '';
if (!empty($lines)) { if (!empty($lines)) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment