mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-13 19:51:30 +00:00
Import latest app updates from streamline
An updated set of source files and initialization was provided by streamline to address issues observed during initial testing. These files have been updated in order to generate a new set of app images.
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* A test class for running all PHP_CodeSniffer unit tests.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests;
|
||||
|
||||
require_once 'Core/AllTests.php';
|
||||
require_once 'Standards/AllSniffs.php';
|
||||
|
||||
// PHPUnit 7 made the TestSuite run() method incompatible with
|
||||
// older PHPUnit versions due to return type hints, so maintain
|
||||
// two different suite objects.
|
||||
$phpunit7 = false;
|
||||
if (class_exists('\PHPUnit\Runner\Version') === true) {
|
||||
$version = \PHPUnit\Runner\Version::id();
|
||||
if (version_compare($version, '7.0', '>=') === true) {
|
||||
$phpunit7 = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($phpunit7 === true) {
|
||||
include_once 'TestSuite7.php';
|
||||
} else {
|
||||
include_once 'TestSuite.php';
|
||||
}
|
||||
|
||||
class PHP_CodeSniffer_AllTests
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Add all PHP_CodeSniffer test suites into a single test suite.
|
||||
*
|
||||
* @return \PHPUnit\Framework\TestSuite
|
||||
*/
|
||||
public static function suite()
|
||||
{
|
||||
$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'] = [];
|
||||
$GLOBALS['PHP_CODESNIFFER_TEST_DIRS'] = [];
|
||||
|
||||
// Use a special PHP_CodeSniffer test suite so that we can
|
||||
// unset our autoload function after the run.
|
||||
$suite = new TestSuite('PHP CodeSniffer');
|
||||
|
||||
$suite->addTest(Core\AllTests::suite());
|
||||
$suite->addTest(Standards\AllSniffs::suite());
|
||||
|
||||
return $suite;
|
||||
|
||||
}//end suite()
|
||||
|
||||
|
||||
}//end class
|
||||
-196
@@ -1,196 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Config class for use in the tests.
|
||||
*
|
||||
* The Config class contains a number of static properties.
|
||||
* As the value of these static properties will be retained between instantiations of the class,
|
||||
* config values set in one test can influence the results for another test, which makes tests unstable.
|
||||
*
|
||||
* This class is a "double" of the Config class which prevents this from happening.
|
||||
* In _most_ cases, tests should be using this class instead of the "normal" Config,
|
||||
* with the exception of select tests for the Config class itself.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2024 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests;
|
||||
|
||||
use PHP_CodeSniffer\Config;
|
||||
use ReflectionProperty;
|
||||
|
||||
final class ConfigDouble extends Config
|
||||
{
|
||||
|
||||
/**
|
||||
* Whether or not the setting of a standard should be skipped.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $skipSettingStandard = false;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a clean Config object and populates it with command line values.
|
||||
*
|
||||
* @param array<string> $cliArgs An array of values gathered from CLI args.
|
||||
* @param bool $skipSettingStandard Whether to skip setting a standard to prevent
|
||||
* the Config class trying to auto-discover a ruleset file.
|
||||
* Should only be set to `true` for tests which actually test
|
||||
* the ruleset auto-discovery.
|
||||
* Note: there is no need to set this to `true` when a standard
|
||||
* is being passed via the `$cliArgs`. Those settings will always
|
||||
* respected.
|
||||
* Defaults to `false`. Will result in the standard being set
|
||||
* to "PSR1" if not provided via `$cliArgs`.
|
||||
* @param bool $skipSettingReportWidth Whether to skip setting a report-width to prevent
|
||||
* the Config class trying to auto-discover the screen width.
|
||||
* Should only be set to `true` for tests which actually test
|
||||
* the screen width auto-discovery.
|
||||
* Note: there is no need to set this to `true` when a report-width
|
||||
* is being passed via the `$cliArgs`. Those settings will always
|
||||
* respected.
|
||||
* Defaults to `false`. Will result in the reportWidth being set
|
||||
* to "80" if not provided via `$cliArgs`.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $cliArgs=[], $skipSettingStandard=false, $skipSettingReportWidth=false)
|
||||
{
|
||||
$this->skipSettingStandard = $skipSettingStandard;
|
||||
|
||||
$this->resetSelectProperties();
|
||||
$this->preventReadingCodeSnifferConfFile();
|
||||
|
||||
parent::__construct($cliArgs);
|
||||
|
||||
if ($skipSettingReportWidth !== true) {
|
||||
$this->preventAutoDiscoveryScreenWidth();
|
||||
}
|
||||
|
||||
}//end __construct()
|
||||
|
||||
|
||||
/**
|
||||
* Sets the command line values and optionally prevents a file system search for a custom ruleset.
|
||||
*
|
||||
* @param array<string> $args An array of command line arguments to set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setCommandLineValues($args)
|
||||
{
|
||||
parent::setCommandLineValues($args);
|
||||
|
||||
if ($this->skipSettingStandard !== true) {
|
||||
$this->preventSearchingForRuleset();
|
||||
}
|
||||
|
||||
}//end setCommandLineValues()
|
||||
|
||||
|
||||
/**
|
||||
* Reset a few properties on the Config class to their default values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function resetSelectProperties()
|
||||
{
|
||||
$this->setStaticConfigProperty('overriddenDefaults', []);
|
||||
$this->setStaticConfigProperty('executablePaths', []);
|
||||
|
||||
}//end resetSelectProperties()
|
||||
|
||||
|
||||
/**
|
||||
* Prevent the values in a potentially available user-specific `CodeSniffer.conf` file
|
||||
* from influencing the tests.
|
||||
*
|
||||
* This also prevents some file system calls which can influence the test runtime.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function preventReadingCodeSnifferConfFile()
|
||||
{
|
||||
$this->setStaticConfigProperty('configData', []);
|
||||
$this->setStaticConfigProperty('configDataFile', '');
|
||||
|
||||
}//end preventReadingCodeSnifferConfFile()
|
||||
|
||||
|
||||
/**
|
||||
* Prevent searching for a custom ruleset by setting a standard, but only if the test
|
||||
* being run doesn't set a standard itself.
|
||||
*
|
||||
* This also prevents some file system calls which can influence the test runtime.
|
||||
*
|
||||
* The standard being set is the smallest one available so the ruleset initialization
|
||||
* will be the fastest possible.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function preventSearchingForRuleset()
|
||||
{
|
||||
$overriddenDefaults = $this->getStaticConfigProperty('overriddenDefaults');
|
||||
if (isset($overriddenDefaults['standards']) === false) {
|
||||
$this->standards = ['PSR1'];
|
||||
$overriddenDefaults['standards'] = true;
|
||||
}
|
||||
|
||||
self::setStaticConfigProperty('overriddenDefaults', $overriddenDefaults);
|
||||
|
||||
}//end preventSearchingForRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Prevent a call to stty to figure out the screen width, but only if the test being run
|
||||
* doesn't set a report width itself.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function preventAutoDiscoveryScreenWidth()
|
||||
{
|
||||
$settings = $this->getSettings();
|
||||
if ($settings['reportWidth'] === 'auto') {
|
||||
$this->reportWidth = self::DEFAULT_REPORT_WIDTH;
|
||||
}
|
||||
|
||||
}//end preventAutoDiscoveryScreenWidth()
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to retrieve the value of a private static property on the Config class.
|
||||
*
|
||||
* @param string $name The name of the property to retrieve.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function getStaticConfigProperty($name)
|
||||
{
|
||||
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
|
||||
$property->setAccessible(true);
|
||||
return $property->getValue();
|
||||
|
||||
}//end getStaticConfigProperty()
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to set the value of a private static property on the Config class.
|
||||
*
|
||||
* @param string $name The name of the property to set.
|
||||
* @param mixed $value The value to set the property to.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setStaticConfigProperty($name, $value)
|
||||
{
|
||||
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
|
||||
$property->setAccessible(true);
|
||||
$property->setValue(null, $value);
|
||||
$property->setAccessible(false);
|
||||
|
||||
}//end setStaticConfigProperty()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-185
@@ -1,185 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class to use when testing utility methods.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Files\DummyFile;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class AbstractMethodUnitTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The file extension of the test case file (without leading dot).
|
||||
*
|
||||
* This allows child classes to overrule the default `inc` with, for instance,
|
||||
* `js` or `css` when applicable.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $fileExtension = 'inc';
|
||||
|
||||
/**
|
||||
* The tab width setting to use when tokenizing the file.
|
||||
*
|
||||
* This allows for test case files to use a different tab width than the default.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected static $tabWidth = 4;
|
||||
|
||||
/**
|
||||
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Files\File
|
||||
*/
|
||||
protected static $phpcsFile;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
|
||||
*
|
||||
* The test case file for a unit test class has to be in the same directory
|
||||
* directory and use the same file name as the test class, using the .inc extension.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initializeFile()
|
||||
{
|
||||
$config = new ConfigDouble();
|
||||
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
|
||||
$config->tabWidth = static::$tabWidth;
|
||||
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
// Default to a file with the same name as the test class. Extension is property based.
|
||||
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
|
||||
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
|
||||
$pathToTestFile = realpath(__DIR__).$relativePath.'.'.static::$fileExtension;
|
||||
|
||||
// Make sure the file gets parsed correctly based on the file type.
|
||||
$contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL;
|
||||
$contents .= file_get_contents($pathToTestFile);
|
||||
|
||||
self::$phpcsFile = new DummyFile($contents, $ruleset, $config);
|
||||
self::$phpcsFile->process();
|
||||
|
||||
}//end initializeFile()
|
||||
|
||||
|
||||
/**
|
||||
* Get the token pointer for a target token based on a specific comment found on the line before.
|
||||
*
|
||||
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
|
||||
* distinguish between comments used *in* a test and test delimiters.
|
||||
*
|
||||
* @param string $commentString The delimiter comment to look for.
|
||||
* @param int|string|array $tokenType The type of token(s) to look for.
|
||||
* @param string $tokenContent Optional. The token content for the target token.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getTargetToken($commentString, $tokenType, $tokenContent=null)
|
||||
{
|
||||
return self::getTargetTokenFromFile(self::$phpcsFile, $commentString, $tokenType, $tokenContent);
|
||||
|
||||
}//end getTargetToken()
|
||||
|
||||
|
||||
/**
|
||||
* Get the token pointer for a target token based on a specific comment found on the line before.
|
||||
*
|
||||
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
|
||||
* distinguish between comments used *in* a test and test delimiters.
|
||||
*
|
||||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file to find the token in.
|
||||
* @param string $commentString The delimiter comment to look for.
|
||||
* @param int|string|array $tokenType The type of token(s) to look for.
|
||||
* @param string $tokenContent Optional. The token content for the target token.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getTargetTokenFromFile(File $phpcsFile, $commentString, $tokenType, $tokenContent=null)
|
||||
{
|
||||
$start = ($phpcsFile->numTokens - 1);
|
||||
$comment = $phpcsFile->findPrevious(
|
||||
T_COMMENT,
|
||||
$start,
|
||||
null,
|
||||
false,
|
||||
$commentString
|
||||
);
|
||||
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$end = ($start + 1);
|
||||
|
||||
// Limit the token finding to between this and the next delimiter comment.
|
||||
for ($i = ($comment + 1); $i < $end; $i++) {
|
||||
if ($tokens[$i]['code'] !== T_COMMENT) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stripos($tokens[$i]['content'], '/* test') === 0) {
|
||||
$end = $i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$target = $phpcsFile->findNext(
|
||||
$tokenType,
|
||||
($comment + 1),
|
||||
$end,
|
||||
false,
|
||||
$tokenContent
|
||||
);
|
||||
|
||||
if ($target === false) {
|
||||
$msg = 'Failed to find test target token for comment string: '.$commentString;
|
||||
if ($tokenContent !== null) {
|
||||
$msg .= ' With token content: '.$tokenContent;
|
||||
}
|
||||
|
||||
self::assertFalse(true, $msg);
|
||||
}
|
||||
|
||||
return $target;
|
||||
|
||||
}//end getTargetTokenFromFile()
|
||||
|
||||
|
||||
/**
|
||||
* Helper method to tell PHPUnit to expect a PHPCS RuntimeException in a PHPUnit cross-version
|
||||
* compatible manner.
|
||||
*
|
||||
* @param string $message The expected exception message.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function expectRunTimeException($message)
|
||||
{
|
||||
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
|
||||
|
||||
if (method_exists($this, 'expectException') === true) {
|
||||
// PHPUnit 5+.
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionMessage($message);
|
||||
} else {
|
||||
// PHPUnit 4.
|
||||
$this->setExpectedException($exception, $message);
|
||||
}
|
||||
|
||||
}//end expectRunTimeException()
|
||||
|
||||
|
||||
}//end class
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* A test class for testing the core.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core;
|
||||
|
||||
use PHP_CodeSniffer\Tests\FileList;
|
||||
use PHPUnit\TextUI\TestRunner;
|
||||
use PHPUnit\Framework\TestSuite;
|
||||
|
||||
class AllTests
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Prepare the test runner.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function main()
|
||||
{
|
||||
TestRunner::run(self::suite());
|
||||
|
||||
}//end main()
|
||||
|
||||
|
||||
/**
|
||||
* Add all core unit tests into a test suite.
|
||||
*
|
||||
* @return \PHPUnit\Framework\TestSuite
|
||||
*/
|
||||
public static function suite()
|
||||
{
|
||||
$suite = new TestSuite('PHP CodeSniffer Core');
|
||||
|
||||
$testFileIterator = new FileList(__DIR__, '', '`Test\.php$`Di');
|
||||
foreach ($testFileIterator->fileIterator as $file) {
|
||||
if (strpos($file, 'AbstractMethodUnitTest.php') !== false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
include_once $file;
|
||||
|
||||
$class = str_replace(__DIR__, '', $file);
|
||||
$class = str_replace('.php', '', $class);
|
||||
$class = str_replace('/', '\\', $class);
|
||||
$class = 'PHP_CodeSniffer\Tests\Core'.$class;
|
||||
|
||||
$suite->addTestSuite($class);
|
||||
}
|
||||
|
||||
return $suite;
|
||||
|
||||
}//end suite()
|
||||
|
||||
|
||||
}//end class
|
||||
-126
@@ -1,126 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Autoload::determineLoadedClass method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Autoloader;
|
||||
|
||||
use PHP_CodeSniffer\Autoload;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Autoload::determineLoadedClass method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Autoload::determineLoadedClass
|
||||
*/
|
||||
final class DetermineLoadedClassTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Load the test files.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function includeFixture()
|
||||
{
|
||||
include __DIR__.'/TestFiles/Sub/C.inc';
|
||||
|
||||
}//end includeFixture()
|
||||
|
||||
|
||||
/**
|
||||
* Test for when class list is ordered.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOrdered()
|
||||
{
|
||||
$classesBeforeLoad = [
|
||||
'classes' => [],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$classesAfterLoad = [
|
||||
'classes' => [
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\A',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\B',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C',
|
||||
],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$className = Autoload::determineLoadedClass($classesBeforeLoad, $classesAfterLoad);
|
||||
$this->assertEquals('PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C', $className);
|
||||
|
||||
}//end testOrdered()
|
||||
|
||||
|
||||
/**
|
||||
* Test for when class list is out of order.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnordered()
|
||||
{
|
||||
$classesBeforeLoad = [
|
||||
'classes' => [],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$classesAfterLoad = [
|
||||
'classes' => [
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\A',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\B',
|
||||
],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$className = Autoload::determineLoadedClass($classesBeforeLoad, $classesAfterLoad);
|
||||
$this->assertEquals('PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C', $className);
|
||||
|
||||
$classesAfterLoad = [
|
||||
'classes' => [
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\A',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\B',
|
||||
],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$className = Autoload::determineLoadedClass($classesBeforeLoad, $classesAfterLoad);
|
||||
$this->assertEquals('PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C', $className);
|
||||
|
||||
$classesAfterLoad = [
|
||||
'classes' => [
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\A',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\C',
|
||||
'PHP_CodeSniffer\Tests\Core\Autoloader\B',
|
||||
],
|
||||
'interfaces' => [],
|
||||
'traits' => [],
|
||||
];
|
||||
|
||||
$className = Autoload::determineLoadedClass($classesBeforeLoad, $classesAfterLoad);
|
||||
$this->assertEquals('PHP_CodeSniffer\Tests\Core\Autoloader\Sub\C', $className);
|
||||
|
||||
}//end testUnordered()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-3
@@ -1,3 +0,0 @@
|
||||
<?php
|
||||
namespace PHP_CodeSniffer\Tests\Core\Autoloader;
|
||||
class A {}
|
||||
Vendored
-4
@@ -1,4 +0,0 @@
|
||||
<?php
|
||||
namespace PHP_CodeSniffer\Tests\Core\Autoloader;
|
||||
require 'A.inc';
|
||||
class B extends A {}
|
||||
Vendored
-4
@@ -1,4 +0,0 @@
|
||||
<?php
|
||||
namespace PHP_CodeSniffer\Tests\Core\Autoloader;
|
||||
require 'B.inc';
|
||||
class C extends B {}
|
||||
Vendored
-5
@@ -1,5 +0,0 @@
|
||||
<?php
|
||||
namespace PHP_CodeSniffer\Tests\Core\Autoloader\Sub;
|
||||
require __DIR__.'/../C.inc';
|
||||
use PHP_CodeSniffer\Tests\Core\Autoloader\C as ParentC;
|
||||
class C extends ParentC {}
|
||||
Vendored
-332
@@ -1,332 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2006-2023 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Config;
|
||||
|
||||
use PHP_CodeSniffer\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionProperty;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Config reportWidth value.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__get
|
||||
*/
|
||||
final class ReportWidthTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Set static properties in the Config class to prevent tests influencing each other.
|
||||
*
|
||||
* @before
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function cleanConfig()
|
||||
{
|
||||
// Set to the property's default value to clear out potentially set values from other tests.
|
||||
self::setStaticProperty('executablePaths', []);
|
||||
|
||||
// Set to a usable value to circumvent Config trying to find a phpcs.xml config file.
|
||||
self::setStaticProperty('overriddenDefaults', ['standards' => ['PSR1']]);
|
||||
|
||||
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
|
||||
// from being read and influencing the tests.
|
||||
self::setStaticProperty('configData', []);
|
||||
self::setStaticProperty('configDataFile', '');
|
||||
|
||||
}//end cleanConfig()
|
||||
|
||||
|
||||
/**
|
||||
* Clean up after each finished test.
|
||||
*
|
||||
* @after
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetConfig()
|
||||
{
|
||||
$_SERVER['argv'] = [];
|
||||
|
||||
}//end resetConfig()
|
||||
|
||||
|
||||
/**
|
||||
* Reset the static properties in the Config class to their true defaults to prevent this class
|
||||
* from influencing other tests.
|
||||
*
|
||||
* @afterClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function resetConfigToDefaults()
|
||||
{
|
||||
self::setStaticProperty('overriddenDefaults', []);
|
||||
self::setStaticProperty('executablePaths', []);
|
||||
self::setStaticProperty('configData', null);
|
||||
self::setStaticProperty('configDataFile', null);
|
||||
$_SERVER['argv'] = [];
|
||||
|
||||
}//end resetConfigToDefaults()
|
||||
|
||||
|
||||
/**
|
||||
* Test that report width without overrules will always be set to a non-0 positive integer.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::restoreDefaults
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthDefault()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
|
||||
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
|
||||
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
|
||||
|
||||
}//end testReportWidthDefault()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set to a non-0 positive integer when not found in the CodeSniffer.conf file.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::restoreDefaults
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
|
||||
{
|
||||
$phpCodeSnifferConfig = [
|
||||
'default_standard' => 'PSR2',
|
||||
'show_warnings' => '0',
|
||||
];
|
||||
|
||||
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
|
||||
|
||||
$config = new Config();
|
||||
|
||||
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
|
||||
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
|
||||
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
|
||||
|
||||
}//end testReportWidthWillBeSetFromAutoWhenNotFoundInConfFile()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set correctly when found in the CodeSniffer.conf file.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::getConfigData
|
||||
* @covers \PHP_CodeSniffer\Config::restoreDefaults
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthCanBeSetFromConfFile()
|
||||
{
|
||||
$phpCodeSnifferConfig = [
|
||||
'default_standard' => 'PSR2',
|
||||
'report_width' => '120',
|
||||
];
|
||||
|
||||
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
|
||||
|
||||
$config = new Config();
|
||||
$this->assertSame(120, $config->reportWidth);
|
||||
|
||||
}//end testReportWidthCanBeSetFromConfFile()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set correctly when passed as a CLI argument.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::processLongArgument
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthCanBeSetFromCLI()
|
||||
{
|
||||
$_SERVER['argv'] = [
|
||||
'phpcs',
|
||||
'--report-width=100',
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$this->assertSame(100, $config->reportWidth);
|
||||
|
||||
}//end testReportWidthCanBeSetFromCLI()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set correctly when multiple report widths are passed on the CLI.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::processLongArgument
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthWhenSetFromCLIFirstValuePrevails()
|
||||
{
|
||||
$_SERVER['argv'] = [
|
||||
'phpcs',
|
||||
'--report-width=100',
|
||||
'--report-width=200',
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$this->assertSame(100, $config->reportWidth);
|
||||
|
||||
}//end testReportWidthWhenSetFromCLIFirstValuePrevails()
|
||||
|
||||
|
||||
/**
|
||||
* Test that a report width passed as a CLI argument will overrule a report width set in a CodeSniffer.conf file.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
* @covers \PHP_CodeSniffer\Config::processLongArgument
|
||||
* @covers \PHP_CodeSniffer\Config::getConfigData
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthSetFromCLIOverrulesConfFile()
|
||||
{
|
||||
$phpCodeSnifferConfig = [
|
||||
'default_standard' => 'PSR2',
|
||||
'report_format' => 'summary',
|
||||
'show_warnings' => '0',
|
||||
'show_progress' => '1',
|
||||
'report_width' => '120',
|
||||
];
|
||||
|
||||
$this->setStaticProperty('configData', $phpCodeSnifferConfig);
|
||||
|
||||
$cliArgs = [
|
||||
'phpcs',
|
||||
'--report-width=180',
|
||||
];
|
||||
|
||||
$config = new Config($cliArgs);
|
||||
$this->assertSame(180, $config->reportWidth);
|
||||
|
||||
}//end testReportWidthSetFromCLIOverrulesConfFile()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set to a non-0 positive integer when set to "auto".
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthInputHandlingForAuto()
|
||||
{
|
||||
$config = new Config();
|
||||
$config->reportWidth = 'auto';
|
||||
|
||||
// Can't test the exact value as "auto" will resolve differently depending on the machine running the tests.
|
||||
$this->assertTrue(is_int($config->reportWidth), 'Report width is not an integer');
|
||||
$this->assertGreaterThan(0, $config->reportWidth, 'Report width is not greater than 0');
|
||||
|
||||
}//end testReportWidthInputHandlingForAuto()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the report width will be set correctly for various types of input.
|
||||
*
|
||||
* @param mixed $input Input value received.
|
||||
* @param int $expected Expected report width.
|
||||
*
|
||||
* @dataProvider dataReportWidthInputHandling
|
||||
* @covers \PHP_CodeSniffer\Config::__set
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthInputHandling($input, $expected)
|
||||
{
|
||||
$config = new Config();
|
||||
$config->reportWidth = $input;
|
||||
|
||||
$this->assertSame($expected, $config->reportWidth);
|
||||
|
||||
}//end testReportWidthInputHandling()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
public static function dataReportWidthInputHandling()
|
||||
{
|
||||
return [
|
||||
'No value (empty string)' => [
|
||||
'value' => '',
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: invalid input type null' => [
|
||||
'value' => null,
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: invalid input type false' => [
|
||||
'value' => false,
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: invalid input type float' => [
|
||||
'value' => 100.50,
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: invalid string value "invalid"' => [
|
||||
'value' => 'invalid',
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: invalid string value, non-integer string "50.25"' => [
|
||||
'value' => '50.25',
|
||||
'expected' => Config::DEFAULT_REPORT_WIDTH,
|
||||
],
|
||||
'Value: valid numeric string value' => [
|
||||
'value' => '250',
|
||||
'expected' => 250,
|
||||
],
|
||||
'Value: valid int value' => [
|
||||
'value' => 220,
|
||||
'expected' => 220,
|
||||
],
|
||||
'Value: negative int value becomes positive int' => [
|
||||
'value' => -180,
|
||||
'expected' => 180,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataReportWidthInputHandling()
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to set a static property on the Config class.
|
||||
*
|
||||
* @param string $name The name of the property to set.
|
||||
* @param mixed $value The value to set the property to.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function setStaticProperty($name, $value)
|
||||
{
|
||||
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
|
||||
$property->setAccessible(true);
|
||||
$property->setValue(null, $value);
|
||||
$property->setAccessible(false);
|
||||
|
||||
}//end setStaticProperty()
|
||||
|
||||
|
||||
}//end class
|
||||
-1278
File diff suppressed because it is too large
Load Diff
Vendored
-105
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testSimpleAssignment */
|
||||
$a = false;
|
||||
|
||||
/* testControlStructure */
|
||||
while(true) {}
|
||||
$a = 1;
|
||||
|
||||
/* testClosureAssignment */
|
||||
$a = function($b=false;){};
|
||||
|
||||
/* testHeredocFunctionArg */
|
||||
myFunction(<<<END
|
||||
Foo
|
||||
END
|
||||
, 'bar');
|
||||
|
||||
/* testSwitch */
|
||||
switch ($a) {
|
||||
case 1: {break;}
|
||||
default: {break;}
|
||||
}
|
||||
|
||||
/* testStatementAsArrayValue */
|
||||
$a = [new Datetime];
|
||||
$a = array(new Datetime);
|
||||
$a = new Datetime;
|
||||
|
||||
/* testUseGroup */
|
||||
use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
|
||||
|
||||
$a = [
|
||||
/* testArrowFunctionArrayValue */
|
||||
'a' => fn() => return 1,
|
||||
'b' => fn() => return 1,
|
||||
];
|
||||
|
||||
/* testStaticArrowFunction */
|
||||
static fn ($a) => $a;
|
||||
|
||||
return 0;
|
||||
|
||||
/* testArrowFunctionReturnValue */
|
||||
fn(): array => [a($a, $b)];
|
||||
|
||||
/* testArrowFunctionAsArgument */
|
||||
$foo = foo(
|
||||
fn() => bar()
|
||||
);
|
||||
|
||||
/* testArrowFunctionWithArrayAsArgument */
|
||||
$foo = foo(
|
||||
fn() => [$row[0], $row[3]]
|
||||
);
|
||||
|
||||
$match = match ($a) {
|
||||
/* testMatchCase */
|
||||
1 => 'foo',
|
||||
/* testMatchDefault */
|
||||
default => 'bar'
|
||||
};
|
||||
|
||||
$match = match ($a) {
|
||||
/* testMatchMultipleCase */
|
||||
1, 2, => $a * $b,
|
||||
/* testMatchDefaultComma */
|
||||
default, => 'something'
|
||||
};
|
||||
|
||||
match ($pressedKey) {
|
||||
/* testMatchFunctionCall */
|
||||
Key::RETURN_ => save($value, $user)
|
||||
};
|
||||
|
||||
$result = match (true) {
|
||||
/* testMatchFunctionCallArm */
|
||||
str_contains($text, 'Welcome') || str_contains($text, 'Hello') => 'en',
|
||||
str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour') => 'fr',
|
||||
default => 'pl'
|
||||
};
|
||||
|
||||
/* testMatchClosure */
|
||||
$result = match ($key) {
|
||||
1 => function($a, $b) {},
|
||||
2 => function($b, $c) {},
|
||||
};
|
||||
|
||||
/* testMatchArray */
|
||||
$result = match ($key) {
|
||||
1 => [1,2,3],
|
||||
2 => [1 => one(), 2 => two()],
|
||||
};
|
||||
|
||||
/* testNestedMatch */
|
||||
$result = match ($key) {
|
||||
1 => match ($key) {
|
||||
1 => 'one',
|
||||
2 => 'two',
|
||||
},
|
||||
2 => match ($key) {
|
||||
1 => 'two',
|
||||
2 => 'one',
|
||||
},
|
||||
};
|
||||
Vendored
-420
@@ -1,420 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findEndOfStatement method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findEndOfStatement method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::findEndOfStatement
|
||||
*/
|
||||
final class FindEndOfStatementTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test a simple assignment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSimpleAssignment()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testSimpleAssignment */', T_VARIABLE);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 5), $found);
|
||||
|
||||
}//end testSimpleAssignment()
|
||||
|
||||
|
||||
/**
|
||||
* Test a direct call to a control structure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testControlStructure()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testControlStructure */', T_WHILE);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 6), $found);
|
||||
|
||||
}//end testControlStructure()
|
||||
|
||||
|
||||
/**
|
||||
* Test the assignment of a closure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testClosureAssignment()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testClosureAssignment */', T_VARIABLE, '$a');
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 13), $found);
|
||||
|
||||
}//end testClosureAssignment()
|
||||
|
||||
|
||||
/**
|
||||
* Test using a heredoc in a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHeredocFunctionArg()
|
||||
{
|
||||
// Find the end of the function.
|
||||
$start = $this->getTargetToken('/* testHeredocFunctionArg */', T_STRING, 'myFunction');
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 10), $found);
|
||||
|
||||
// Find the end of the heredoc.
|
||||
$start += 2;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 4), $found);
|
||||
|
||||
// Find the end of the last arg.
|
||||
$start = ($found + 2);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testHeredocFunctionArg()
|
||||
|
||||
|
||||
/**
|
||||
* Test parts of a switch statement.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSwitch()
|
||||
{
|
||||
// Find the end of the switch.
|
||||
$start = $this->getTargetToken('/* testSwitch */', T_SWITCH);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 28), $found);
|
||||
|
||||
// Find the end of the case.
|
||||
$start += 9;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 8), $found);
|
||||
|
||||
// Find the end of default case.
|
||||
$start += 11;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 6), $found);
|
||||
|
||||
}//end testSwitch()
|
||||
|
||||
|
||||
/**
|
||||
* Test statements that are array values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStatementAsArrayValue()
|
||||
{
|
||||
// Test short array syntax.
|
||||
$start = $this->getTargetToken('/* testStatementAsArrayValue */', T_NEW);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 2), $found);
|
||||
|
||||
// Test long array syntax.
|
||||
$start += 12;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 2), $found);
|
||||
|
||||
// Test same statement outside of array.
|
||||
$start += 10;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 3), $found);
|
||||
|
||||
}//end testStatementAsArrayValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test a use group.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUseGroup()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testUseGroup */', T_USE);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 23), $found);
|
||||
|
||||
}//end testUseGroup()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function as array value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionArrayValue()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_FN);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 9), $found);
|
||||
|
||||
}//end testArrowFunctionArrayValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test static arrow function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStaticArrowFunction()
|
||||
{
|
||||
$static = $this->getTargetToken('/* testStaticArrowFunction */', T_STATIC);
|
||||
$fn = $this->getTargetToken('/* testStaticArrowFunction */', T_FN);
|
||||
|
||||
$endOfStatementStatic = self::$phpcsFile->findEndOfStatement($static);
|
||||
$endOfStatementFn = self::$phpcsFile->findEndOfStatement($fn);
|
||||
|
||||
$this->assertSame($endOfStatementFn, $endOfStatementStatic);
|
||||
|
||||
}//end testStaticArrowFunction()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function with return value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionReturnValue()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_FN);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 18), $found);
|
||||
|
||||
}//end testArrowFunctionReturnValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function used as a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionAsArgument()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 8), $found);
|
||||
|
||||
}//end testArrowFunctionAsArgument()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function with arrays used as a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionWithArrayAsArgument()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 17), $found);
|
||||
|
||||
}//end testArrowFunctionWithArrayAsArgument()
|
||||
|
||||
|
||||
/**
|
||||
* Test simple match expression case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchCase()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchCase */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 5), $found);
|
||||
|
||||
$start = $this->getTargetToken('/* testMatchCase */', T_CONSTANT_ENCAPSED_STRING);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 1), $found);
|
||||
|
||||
}//end testMatchCase()
|
||||
|
||||
|
||||
/**
|
||||
* Test simple match expression default case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchDefault()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchDefault */', T_MATCH_DEFAULT);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 4), $found);
|
||||
|
||||
$start = $this->getTargetToken('/* testMatchDefault */', T_CONSTANT_ENCAPSED_STRING);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testMatchDefault()
|
||||
|
||||
|
||||
/**
|
||||
* Test multiple comma-separated match expression case values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchMultipleCase()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchMultipleCase */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
$this->assertSame(($start + 13), $found);
|
||||
|
||||
$start += 6;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
$this->assertSame(($start + 7), $found);
|
||||
|
||||
}//end testMatchMultipleCase()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression default case with trailing comma.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchDefaultComma()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchDefaultComma */', T_MATCH_DEFAULT);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 5), $found);
|
||||
|
||||
}//end testMatchDefaultComma()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with function call.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchFunctionCall()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchFunctionCall */', T_STRING);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 12), $found);
|
||||
|
||||
$start += 8;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 1), $found);
|
||||
|
||||
}//end testMatchFunctionCall()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with function call in the arm.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchFunctionCallArm()
|
||||
{
|
||||
// Check the first case.
|
||||
$start = $this->getTargetToken('/* testMatchFunctionCallArm */', T_STRING);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 21), $found);
|
||||
|
||||
// Check the second case.
|
||||
$start += 24;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 21), $found);
|
||||
|
||||
}//end testMatchFunctionCallArm()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with closure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchClosure()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchClosure */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 14), $found);
|
||||
|
||||
$start += 17;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 14), $found);
|
||||
|
||||
}//end testMatchClosure()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with array declaration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchArray()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchArray */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 11), $found);
|
||||
|
||||
$start += 14;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 22), $found);
|
||||
|
||||
}//end testMatchArray()
|
||||
|
||||
|
||||
/**
|
||||
* Test nested match expressions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedMatch()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testNestedMatch */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 30), $found);
|
||||
|
||||
$start += 21;
|
||||
$found = self::$phpcsFile->findEndOfStatement($start);
|
||||
|
||||
$this->assertSame(($start + 5), $found);
|
||||
|
||||
}//end testNestedMatch()
|
||||
|
||||
|
||||
}//end class
|
||||
docker/streamline-src/vendor/squizlabs/php_codesniffer/tests/Core/File/FindExtendedClassNameTest.inc
Vendored
-52
@@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testNotAClass */
|
||||
function notAClass() {}
|
||||
|
||||
/* testNonExtendedClass */
|
||||
class testFECNNonExtendedClass {}
|
||||
|
||||
/* testExtendsUnqualifiedClass */
|
||||
class testFECNExtendedClass extends testFECNClass {}
|
||||
|
||||
/* testExtendsFullyQualifiedClass */
|
||||
class testFECNNamespacedClass extends \PHP_CodeSniffer\Tests\Core\File\testFECNClass {}
|
||||
|
||||
/* testExtendsPartiallyQualifiedClass */
|
||||
class testFECNQualifiedClass extends Core\File\RelativeClass {}
|
||||
|
||||
/* testNonExtendedInterface */
|
||||
interface testFECNInterface {}
|
||||
|
||||
/* testInterfaceExtendsUnqualifiedInterface */
|
||||
interface testInterfaceThatExtendsInterface extends testFECNInterface{}
|
||||
|
||||
/* testInterfaceExtendsFullyQualifiedInterface */
|
||||
interface testInterfaceThatExtendsFQCNInterface extends \PHP_CodeSniffer\Tests\Core\File\testFECNInterface{}
|
||||
|
||||
/* testExtendedAnonClass */
|
||||
$anon = new class( $a, $b ) extends testFECNExtendedAnonClass {};
|
||||
|
||||
/* testNestedExtendedClass */
|
||||
class testFECNNestedExtendedClass {
|
||||
public function someMethod() {
|
||||
/* testNestedExtendedAnonClass */
|
||||
$anon = new class extends testFECNAnonClass {};
|
||||
}
|
||||
}
|
||||
|
||||
/* testClassThatExtendsAndImplements */
|
||||
class testFECNClassThatExtendsAndImplements extends testFECNClass implements InterfaceA, InterfaceB {}
|
||||
|
||||
/* testClassThatImplementsAndExtends */
|
||||
class testFECNClassThatImplementsAndExtends implements InterfaceA, InterfaceB extends testFECNClass {}
|
||||
|
||||
/* testInterfaceMultiExtends */
|
||||
interface Multi extends \Package\FooInterface, \BarInterface {};
|
||||
|
||||
/* testMissingExtendsName */
|
||||
class testMissingExtendsName extends { /* missing classname */ } // Intentional parse error.
|
||||
|
||||
// Intentional parse error. Has to be the last test in the file.
|
||||
/* testParseError */
|
||||
class testParseError extends testFECNClass
|
||||
docker/streamline-src/vendor/squizlabs/php_codesniffer/tests/Core/File/FindExtendedClassNameTest.php
Vendored
-145
@@ -1,145 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findExtendedClassName method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findExtendedClassName method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::findExtendedClassName
|
||||
*/
|
||||
final class FindExtendedClassNameTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a `false` result when a non-existent token is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonExistentToken()
|
||||
{
|
||||
$result = self::$phpcsFile->findExtendedClassName(100000);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNonExistentToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a `false` result when a token other than one of the supported tokens is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNotAClass()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testNotAClass */', [T_FUNCTION]);
|
||||
$result = self::$phpcsFile->findExtendedClassName($token);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNotAClass()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the name of the class being extended by another class
|
||||
* (or interface).
|
||||
*
|
||||
* @param string $identifier Comment which precedes the test case.
|
||||
* @param string|false $expected Expected function output.
|
||||
*
|
||||
* @dataProvider dataExtendedClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFindExtendedClassName($identifier, $expected)
|
||||
{
|
||||
$OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE]);
|
||||
$result = self::$phpcsFile->findExtendedClassName($OOToken);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testFindExtendedClassName()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider for the FindExtendedClassName test.
|
||||
*
|
||||
* @see testFindExtendedClassName()
|
||||
*
|
||||
* @return array<string, array<string, string|false>>
|
||||
*/
|
||||
public static function dataExtendedClass()
|
||||
{
|
||||
return [
|
||||
'class does not extend' => [
|
||||
'identifier' => '/* testNonExtendedClass */',
|
||||
'expected' => false,
|
||||
],
|
||||
'class extends unqualified class' => [
|
||||
'identifier' => '/* testExtendsUnqualifiedClass */',
|
||||
'expected' => 'testFECNClass',
|
||||
],
|
||||
'class extends fully qualified class' => [
|
||||
'identifier' => '/* testExtendsFullyQualifiedClass */',
|
||||
'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNClass',
|
||||
],
|
||||
'class extends partially qualified class' => [
|
||||
'identifier' => '/* testExtendsPartiallyQualifiedClass */',
|
||||
'expected' => 'Core\File\RelativeClass',
|
||||
],
|
||||
'interface does not extend' => [
|
||||
'identifier' => '/* testNonExtendedInterface */',
|
||||
'expected' => false,
|
||||
],
|
||||
'interface extends unqualified interface' => [
|
||||
'identifier' => '/* testInterfaceExtendsUnqualifiedInterface */',
|
||||
'expected' => 'testFECNInterface',
|
||||
],
|
||||
'interface extends fully qualified interface' => [
|
||||
'identifier' => '/* testInterfaceExtendsFullyQualifiedInterface */',
|
||||
'expected' => '\PHP_CodeSniffer\Tests\Core\File\testFECNInterface',
|
||||
],
|
||||
'anon class extends unqualified class' => [
|
||||
'identifier' => '/* testExtendedAnonClass */',
|
||||
'expected' => 'testFECNExtendedAnonClass',
|
||||
],
|
||||
'class does not extend but contains anon class which extends' => [
|
||||
'identifier' => '/* testNestedExtendedClass */',
|
||||
'expected' => false,
|
||||
],
|
||||
'anon class extends, nested in non-extended class' => [
|
||||
'identifier' => '/* testNestedExtendedAnonClass */',
|
||||
'expected' => 'testFECNAnonClass',
|
||||
],
|
||||
'class extends and implements' => [
|
||||
'identifier' => '/* testClassThatExtendsAndImplements */',
|
||||
'expected' => 'testFECNClass',
|
||||
],
|
||||
'class implements and extends' => [
|
||||
'identifier' => '/* testClassThatImplementsAndExtends */',
|
||||
'expected' => 'testFECNClass',
|
||||
],
|
||||
'interface extends multiple interfaces (not supported)' => [
|
||||
'identifier' => '/* testInterfaceMultiExtends */',
|
||||
'expected' => '\Package\FooInterface',
|
||||
],
|
||||
'parse error - extends keyword, but no class name' => [
|
||||
'identifier' => '/* testMissingExtendsName */',
|
||||
'expected' => false,
|
||||
],
|
||||
'parse error - live coding - no curly braces' => [
|
||||
'identifier' => '/* testParseError */',
|
||||
'expected' => false,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataExtendedClass()
|
||||
|
||||
|
||||
}//end class
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testNotAClass */
|
||||
function notAClass() {}
|
||||
|
||||
/* testPlainInterface */
|
||||
interface testFIINInterface {}
|
||||
|
||||
/* testNonImplementedClass */
|
||||
class testFIINNonImplementedClass {}
|
||||
|
||||
/* testClassImplementsSingle */
|
||||
class testFIINImplementedClass implements testFIINInterface {}
|
||||
|
||||
/* testClassImplementsMultiple */
|
||||
class testFIINMultiImplementedClass implements testFIINInterface, testFIINInterface2 {}
|
||||
|
||||
/* testImplementsFullyQualified */
|
||||
class testFIINNamespacedClass implements \PHP_CodeSniffer\Tests\Core\File\testFIINInterface {}
|
||||
|
||||
/* testImplementsPartiallyQualified */
|
||||
class testFIINQualifiedClass implements Core\File\RelativeInterface {}
|
||||
|
||||
/* testClassThatExtendsAndImplements */
|
||||
class testFECNClassThatExtendsAndImplements extends testFECNClass implements InterfaceA, \NameSpaced\Cat\InterfaceB {}
|
||||
|
||||
/* testClassThatImplementsAndExtends */
|
||||
class testFECNClassThatImplementsAndExtends implements \InterfaceA, InterfaceB extends testFECNClass {}
|
||||
|
||||
/* testBackedEnumWithoutImplements */
|
||||
enum Suit:string {}
|
||||
|
||||
/* testEnumImplementsSingle */
|
||||
enum Suit implements Colorful {}
|
||||
|
||||
/* testBackedEnumImplementsMulti */
|
||||
enum Suit: string implements Colorful, \Deck {}
|
||||
|
||||
/* testAnonClassImplementsSingle */
|
||||
$anon = class() implements testFIINInterface {}
|
||||
|
||||
/* testMissingImplementsName */
|
||||
class testMissingExtendsName implements { /* missing interface name */ } // Intentional parse error.
|
||||
|
||||
// Intentional parse error. Has to be the last test in the file.
|
||||
/* testParseError */
|
||||
class testParseError implements testInterface
|
||||
-162
@@ -1,162 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames
|
||||
*/
|
||||
final class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a `false` result when a non-existent token is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonExistentToken()
|
||||
{
|
||||
$result = self::$phpcsFile->findImplementedInterfaceNames(100000);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNonExistentToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a `false` result when a token other than one of the supported tokens is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNotAClass()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testNotAClass */', [T_FUNCTION]);
|
||||
$result = self::$phpcsFile->findImplementedInterfaceNames($token);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNotAClass()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the name(s) of the interfaces being implemented by a class.
|
||||
*
|
||||
* @param string $identifier Comment which precedes the test case.
|
||||
* @param array<string>|false $expected Expected function output.
|
||||
*
|
||||
* @dataProvider dataImplementedInterface
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFindImplementedInterfaceNames($identifier, $expected)
|
||||
{
|
||||
$OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE, T_ENUM]);
|
||||
$result = self::$phpcsFile->findImplementedInterfaceNames($OOToken);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testFindImplementedInterfaceNames()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider for the FindImplementedInterfaceNames test.
|
||||
*
|
||||
* @see testFindImplementedInterfaceNames()
|
||||
*
|
||||
* @return array<string, array<string, string|array<string>>>
|
||||
*/
|
||||
public static function dataImplementedInterface()
|
||||
{
|
||||
return [
|
||||
'interface declaration, no implements' => [
|
||||
'identifier' => '/* testPlainInterface */',
|
||||
'expected' => false,
|
||||
],
|
||||
'class does not implement' => [
|
||||
'identifier' => '/* testNonImplementedClass */',
|
||||
'expected' => false,
|
||||
],
|
||||
'class implements single interface, unqualified' => [
|
||||
'identifier' => '/* testClassImplementsSingle */',
|
||||
'expected' => [
|
||||
'testFIINInterface',
|
||||
],
|
||||
],
|
||||
'class implements multiple interfaces' => [
|
||||
'identifier' => '/* testClassImplementsMultiple */',
|
||||
'expected' => [
|
||||
'testFIINInterface',
|
||||
'testFIINInterface2',
|
||||
],
|
||||
],
|
||||
'class implements single interface, fully qualified' => [
|
||||
'identifier' => '/* testImplementsFullyQualified */',
|
||||
'expected' => [
|
||||
'\PHP_CodeSniffer\Tests\Core\File\testFIINInterface',
|
||||
],
|
||||
],
|
||||
'class implements single interface, partially qualified' => [
|
||||
'identifier' => '/* testImplementsPartiallyQualified */',
|
||||
'expected' => [
|
||||
'Core\File\RelativeInterface',
|
||||
],
|
||||
],
|
||||
'class extends and implements' => [
|
||||
'identifier' => '/* testClassThatExtendsAndImplements */',
|
||||
'expected' => [
|
||||
'InterfaceA',
|
||||
'\NameSpaced\Cat\InterfaceB',
|
||||
],
|
||||
],
|
||||
'class implements and extends' => [
|
||||
'identifier' => '/* testClassThatImplementsAndExtends */',
|
||||
'expected' => [
|
||||
'\InterfaceA',
|
||||
'InterfaceB',
|
||||
],
|
||||
],
|
||||
'enum does not implement' => [
|
||||
'identifier' => '/* testBackedEnumWithoutImplements */',
|
||||
'expected' => false,
|
||||
],
|
||||
'enum implements single interface, unqualified' => [
|
||||
'identifier' => '/* testEnumImplementsSingle */',
|
||||
'expected' => [
|
||||
'Colorful',
|
||||
],
|
||||
],
|
||||
'enum implements multiple interfaces, unqualified + fully qualified' => [
|
||||
'identifier' => '/* testBackedEnumImplementsMulti */',
|
||||
'expected' => [
|
||||
'Colorful',
|
||||
'\Deck',
|
||||
],
|
||||
],
|
||||
'anon class implements single interface, unqualified' => [
|
||||
'identifier' => '/* testAnonClassImplementsSingle */',
|
||||
'expected' => [
|
||||
'testFIINInterface',
|
||||
],
|
||||
],
|
||||
'parse error - implements keyword, but no interface name' => [
|
||||
'identifier' => '/* testMissingImplementsName */',
|
||||
'expected' => false,
|
||||
],
|
||||
'parse error - live coding - no curly braces' => [
|
||||
'identifier' => '/* testParseError */',
|
||||
'expected' => false,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataImplementedInterface()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-164
@@ -1,164 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testSimpleAssignment */
|
||||
$a = false;
|
||||
|
||||
/* testFunctionCall */
|
||||
$a = doSomething();
|
||||
|
||||
/* testFunctionCallArgument */
|
||||
$a = doSomething($a, $b);
|
||||
|
||||
/* testControlStructure */
|
||||
while(true) {}
|
||||
$a = 1;
|
||||
|
||||
/* testClosureAssignment */
|
||||
$a = function($b=false;){};
|
||||
|
||||
/* testHeredocFunctionArg */
|
||||
myFunction(<<<END
|
||||
Foo
|
||||
END
|
||||
, 'bar');
|
||||
|
||||
switch ($a) {
|
||||
case 1: {break;}
|
||||
case 2: $foo = true; break;
|
||||
default: {break;}
|
||||
/* testSwitch */
|
||||
}
|
||||
|
||||
/* testStatementAsArrayValue */
|
||||
$a = [new Datetime];
|
||||
$a = array(new Datetime);
|
||||
$a = ['a' => $foo + $bar, 'b' => true];
|
||||
|
||||
/* testUseGroup */
|
||||
use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
|
||||
|
||||
$a = [
|
||||
/* testArrowFunctionArrayValue */
|
||||
'a' => fn() => return 1,
|
||||
'b' => fn() => return 1,
|
||||
];
|
||||
|
||||
/* testStaticArrowFunction */
|
||||
static fn ($a) => $a;
|
||||
|
||||
/* testArrowFunctionReturnValue */
|
||||
fn(): array => [a($a, $b)];
|
||||
|
||||
/* testArrowFunctionAsArgument */
|
||||
$foo = foo(
|
||||
fn() => bar()
|
||||
);
|
||||
|
||||
/* testArrowFunctionWithArrayAsArgument */
|
||||
$foo = foo(
|
||||
fn() => [$row[0], $row[3]]
|
||||
);
|
||||
|
||||
$match = match ($a) {
|
||||
/* testMatchCase */
|
||||
1 => 'foo',
|
||||
/* testMatchDefault */
|
||||
default => 'bar'
|
||||
};
|
||||
|
||||
$match = match ($a) {
|
||||
/* testMatchMultipleCase */
|
||||
1, 2, => $a * $b,
|
||||
/* testMatchDefaultComma */
|
||||
default, => 'something'
|
||||
};
|
||||
|
||||
match ($pressedKey) {
|
||||
/* testMatchFunctionCall */
|
||||
Key::RETURN_ => save($value, $user)
|
||||
};
|
||||
|
||||
$result = match (true) {
|
||||
/* testMatchFunctionCallArm */
|
||||
str_contains($text, 'Welcome') || str_contains($text, 'Hello') => 'en',
|
||||
str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour') => 'fr',
|
||||
default => 'pl'
|
||||
};
|
||||
|
||||
/* testMatchClosure */
|
||||
$result = match ($key) {
|
||||
1 => function($a, $b) {},
|
||||
2 => function($b, $c) {},
|
||||
};
|
||||
|
||||
/* testMatchArray */
|
||||
$result = match ($key) {
|
||||
1 => [1,2,3],
|
||||
2 => [1 => one($a, $b), 2 => two($b, $c)],
|
||||
3 => [],
|
||||
};
|
||||
|
||||
/* testNestedMatch */
|
||||
$result = match ($key) {
|
||||
1 => match ($key) {
|
||||
1 => 'one',
|
||||
2 => 'two',
|
||||
},
|
||||
2 => match ($key) {
|
||||
1 => 'two',
|
||||
2 => 'one',
|
||||
},
|
||||
};
|
||||
|
||||
return 0;
|
||||
|
||||
/* testOpenTag */
|
||||
?>
|
||||
<h1>Test</h1>
|
||||
<?php echo '<h2>', foo(), '</h2>';
|
||||
|
||||
/* testOpenTagWithEcho */
|
||||
?>
|
||||
<h1>Test</h1>
|
||||
<?= '<h2>', foo(), '</h2>';
|
||||
|
||||
$value = [
|
||||
/* testPrecededByArrowFunctionInArray - Expected */
|
||||
Url::make('View Song', fn($song) => $song->url())
|
||||
/* testPrecededByArrowFunctionInArray */
|
||||
->onlyOnDetail(),
|
||||
|
||||
new Panel('Information', [
|
||||
Text::make('Title')
|
||||
]),
|
||||
];
|
||||
|
||||
switch ($foo) {
|
||||
/* testCaseStatement */
|
||||
case 1:
|
||||
/* testInsideCaseStatement */
|
||||
$var = doSomething();
|
||||
/* testInsideCaseBreakStatement */
|
||||
break 2;
|
||||
|
||||
case 2:
|
||||
/* testInsideCaseContinueStatement */
|
||||
continue 2;
|
||||
|
||||
case 3:
|
||||
/* testInsideCaseReturnStatement */
|
||||
return false;
|
||||
|
||||
case 4:
|
||||
/* testInsideCaseExitStatement */
|
||||
exit(1);
|
||||
|
||||
case 5:
|
||||
/* testInsideCaseThrowStatement */
|
||||
throw new Exception();
|
||||
|
||||
/* testDefaultStatement */
|
||||
default:
|
||||
/* testInsideDefaultContinueStatement */
|
||||
continue $var;
|
||||
}
|
||||
Vendored
-640
@@ -1,640 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @copyright 2019-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::findStartOfStatement
|
||||
*/
|
||||
final class FindStartOfStatementTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test a simple assignment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSimpleAssignment()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testSimpleAssignment */', T_SEMICOLON);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 5), $found);
|
||||
|
||||
}//end testSimpleAssignment()
|
||||
|
||||
|
||||
/**
|
||||
* Test a function call.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFunctionCall()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testFunctionCall */', T_CLOSE_PARENTHESIS);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 6), $found);
|
||||
|
||||
}//end testFunctionCall()
|
||||
|
||||
|
||||
/**
|
||||
* Test a function call.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFunctionCallArgument()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testFunctionCallArgument */', T_VARIABLE, '$b');
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testFunctionCallArgument()
|
||||
|
||||
|
||||
/**
|
||||
* Test a direct call to a control structure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testControlStructure()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testControlStructure */', T_CLOSE_CURLY_BRACKET);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 6), $found);
|
||||
|
||||
}//end testControlStructure()
|
||||
|
||||
|
||||
/**
|
||||
* Test the assignment of a closure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testClosureAssignment()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testClosureAssignment */', T_CLOSE_CURLY_BRACKET);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 12), $found);
|
||||
|
||||
}//end testClosureAssignment()
|
||||
|
||||
|
||||
/**
|
||||
* Test using a heredoc in a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHeredocFunctionArg()
|
||||
{
|
||||
// Find the start of the function.
|
||||
$start = $this->getTargetToken('/* testHeredocFunctionArg */', T_SEMICOLON);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 10), $found);
|
||||
|
||||
// Find the start of the heredoc.
|
||||
$start -= 4;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 4), $found);
|
||||
|
||||
// Find the start of the last arg.
|
||||
$start += 2;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testHeredocFunctionArg()
|
||||
|
||||
|
||||
/**
|
||||
* Test parts of a switch statement.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSwitch()
|
||||
{
|
||||
// Find the start of the switch.
|
||||
$start = $this->getTargetToken('/* testSwitch */', T_CLOSE_CURLY_BRACKET);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 47), $found);
|
||||
|
||||
// Find the start of default case.
|
||||
$start -= 5;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 6), $found);
|
||||
|
||||
// Find the start of the second case.
|
||||
$start -= 12;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 5), $found);
|
||||
|
||||
// Find the start of the first case.
|
||||
$start -= 13;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 8), $found);
|
||||
|
||||
// Test inside the first case.
|
||||
$start--;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 1), $found);
|
||||
|
||||
}//end testSwitch()
|
||||
|
||||
|
||||
/**
|
||||
* Test statements that are array values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStatementAsArrayValue()
|
||||
{
|
||||
// Test short array syntax.
|
||||
$start = $this->getTargetToken('/* testStatementAsArrayValue */', T_STRING, 'Datetime');
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 2), $found);
|
||||
|
||||
// Test long array syntax.
|
||||
$start += 12;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 2), $found);
|
||||
|
||||
// Test same statement outside of array.
|
||||
$start++;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 9), $found);
|
||||
|
||||
// Test with an array index.
|
||||
$start += 17;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 5), $found);
|
||||
|
||||
}//end testStatementAsArrayValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test a use group.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUseGroup()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testUseGroup */', T_SEMICOLON);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 23), $found);
|
||||
|
||||
}//end testUseGroup()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function as array value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionArrayValue()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_COMMA);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 9), $found);
|
||||
|
||||
}//end testArrowFunctionArrayValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test static arrow function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testStaticArrowFunction()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testStaticArrowFunction */', T_SEMICOLON);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 11), $found);
|
||||
|
||||
}//end testStaticArrowFunction()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function with return value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionReturnValue()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionReturnValue */', T_SEMICOLON);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 18), $found);
|
||||
|
||||
}//end testArrowFunctionReturnValue()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function used as a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionAsArgument()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionAsArgument */', T_FN);
|
||||
$start += 8;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 8), $found);
|
||||
|
||||
}//end testArrowFunctionAsArgument()
|
||||
|
||||
|
||||
/**
|
||||
* Test arrow function with arrays used as a function argument.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionWithArrayAsArgument()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testArrowFunctionWithArrayAsArgument */', T_FN);
|
||||
$start += 17;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 17), $found);
|
||||
|
||||
}//end testArrowFunctionWithArrayAsArgument()
|
||||
|
||||
|
||||
/**
|
||||
* Test simple match expression case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchCase()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchCase */', T_COMMA);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 1), $found);
|
||||
|
||||
}//end testMatchCase()
|
||||
|
||||
|
||||
/**
|
||||
* Test simple match expression default case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchDefault()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchDefault */', T_CONSTANT_ENCAPSED_STRING, "'bar'");
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testMatchDefault()
|
||||
|
||||
|
||||
/**
|
||||
* Test multiple comma-separated match expression case values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchMultipleCase()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchMultipleCase */', T_MATCH_ARROW);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 6), $found);
|
||||
|
||||
$start += 6;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 4), $found);
|
||||
|
||||
}//end testMatchMultipleCase()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression default case with trailing comma.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchDefaultComma()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchDefaultComma */', T_MATCH_ARROW);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 3), $found);
|
||||
|
||||
$start += 2;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
}//end testMatchDefaultComma()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with function call.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchFunctionCall()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchFunctionCall */', T_CLOSE_PARENTHESIS);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 6), $found);
|
||||
|
||||
}//end testMatchFunctionCall()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with function call in the arm.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchFunctionCallArm()
|
||||
{
|
||||
// Check the first case.
|
||||
$start = $this->getTargetToken('/* testMatchFunctionCallArm */', T_MATCH_ARROW);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 18), $found);
|
||||
|
||||
// Check the second case.
|
||||
$start += 24;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 18), $found);
|
||||
|
||||
}//end testMatchFunctionCallArm()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with closure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchClosure()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testMatchClosure */', T_LNUMBER);
|
||||
$start += 14;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 10), $found);
|
||||
|
||||
$start += 17;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 10), $found);
|
||||
|
||||
}//end testMatchClosure()
|
||||
|
||||
|
||||
/**
|
||||
* Test match expression with array declaration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMatchArray()
|
||||
{
|
||||
// Start of first case statement.
|
||||
$start = $this->getTargetToken('/* testMatchArray */', T_LNUMBER);
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
// Comma after first statement.
|
||||
$start += 11;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
$this->assertSame(($start - 7), $found);
|
||||
|
||||
// Start of second case statement.
|
||||
$start += 3;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
$this->assertSame($start, $found);
|
||||
|
||||
// Comma after first statement.
|
||||
$start += 30;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
$this->assertSame(($start - 26), $found);
|
||||
|
||||
}//end testMatchArray()
|
||||
|
||||
|
||||
/**
|
||||
* Test nested match expressions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedMatch()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testNestedMatch */', T_LNUMBER);
|
||||
$start += 30;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 26), $found);
|
||||
|
||||
$start -= 4;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 1), $found);
|
||||
|
||||
$start -= 3;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 2), $found);
|
||||
|
||||
}//end testNestedMatch()
|
||||
|
||||
|
||||
/**
|
||||
* Test PHP open tag.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOpenTag()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testOpenTag */', T_OPEN_TAG);
|
||||
$start += 2;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 1), $found);
|
||||
|
||||
}//end testOpenTag()
|
||||
|
||||
|
||||
/**
|
||||
* Test PHP short open echo tag.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOpenTagWithEcho()
|
||||
{
|
||||
$start = $this->getTargetToken('/* testOpenTagWithEcho */', T_OPEN_TAG_WITH_ECHO);
|
||||
$start += 3;
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame(($start - 1), $found);
|
||||
|
||||
}//end testOpenTagWithEcho()
|
||||
|
||||
|
||||
/**
|
||||
* Test object call on result of static function call with arrow function as parameter and wrapped within an array.
|
||||
*
|
||||
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2849
|
||||
* @link https://github.com/squizlabs/PHP_CodeSniffer/commit/fbf67efc3fc0c2a355f5585d49f4f6fe160ff2f9
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray()
|
||||
{
|
||||
$expected = $this->getTargetToken('/* testPrecededByArrowFunctionInArray - Expected */', T_STRING, 'Url');
|
||||
|
||||
$start = $this->getTargetToken('/* testPrecededByArrowFunctionInArray */', T_STRING, 'onlyOnDetail');
|
||||
$found = self::$phpcsFile->findStartOfStatement($start);
|
||||
|
||||
$this->assertSame($expected, $found);
|
||||
|
||||
}//end testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray()
|
||||
|
||||
|
||||
/**
|
||||
* Test finding the start of a statement inside a switch control structure case/default statement.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $targets The token to search for after the test marker.
|
||||
* @param string|int $expectedTarget Token code of the expected start of statement stack pointer.
|
||||
*
|
||||
* @link https://github.com/squizlabs/php_codesniffer/issues/3192
|
||||
* @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3186/commits/18a0e54735bb9b3850fec266e5f4c50dacf618ea
|
||||
*
|
||||
* @dataProvider dataFindStartInsideSwitchCaseDefaultStatements
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFindStartInsideSwitchCaseDefaultStatements($testMarker, $targets, $expectedTarget)
|
||||
{
|
||||
$testToken = $this->getTargetToken($testMarker, $targets);
|
||||
$expected = $this->getTargetToken($testMarker, $expectedTarget);
|
||||
|
||||
$found = self::$phpcsFile->findStartOfStatement($testToken);
|
||||
|
||||
$this->assertSame($expected, $found);
|
||||
|
||||
}//end testFindStartInsideSwitchCaseDefaultStatements()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array<string, array<string, int|string>>
|
||||
*/
|
||||
public static function dataFindStartInsideSwitchCaseDefaultStatements()
|
||||
{
|
||||
return [
|
||||
'Case keyword should be start of case statement - case itself' => [
|
||||
'testMarker' => '/* testCaseStatement */',
|
||||
'targets' => T_CASE,
|
||||
'expectedTarget' => T_CASE,
|
||||
],
|
||||
'Case keyword should be start of case statement - number (what\'s being compared)' => [
|
||||
'testMarker' => '/* testCaseStatement */',
|
||||
'targets' => T_LNUMBER,
|
||||
'expectedTarget' => T_CASE,
|
||||
],
|
||||
'Variable should be start of arbitrary assignment statement - variable itself' => [
|
||||
'testMarker' => '/* testInsideCaseStatement */',
|
||||
'targets' => T_VARIABLE,
|
||||
'expectedTarget' => T_VARIABLE,
|
||||
],
|
||||
'Variable should be start of arbitrary assignment statement - equal sign' => [
|
||||
'testMarker' => '/* testInsideCaseStatement */',
|
||||
'targets' => T_EQUAL,
|
||||
'expectedTarget' => T_VARIABLE,
|
||||
],
|
||||
'Variable should be start of arbitrary assignment statement - function call' => [
|
||||
'testMarker' => '/* testInsideCaseStatement */',
|
||||
'targets' => T_STRING,
|
||||
'expectedTarget' => T_VARIABLE,
|
||||
],
|
||||
'Break should be start for contents of the break statement - contents' => [
|
||||
'testMarker' => '/* testInsideCaseBreakStatement */',
|
||||
'targets' => T_LNUMBER,
|
||||
'expectedTarget' => T_BREAK,
|
||||
],
|
||||
'Continue should be start for contents of the continue statement - contents' => [
|
||||
'testMarker' => '/* testInsideCaseContinueStatement */',
|
||||
'targets' => T_LNUMBER,
|
||||
'expectedTarget' => T_CONTINUE,
|
||||
],
|
||||
'Return should be start for contents of the return statement - contents' => [
|
||||
'testMarker' => '/* testInsideCaseReturnStatement */',
|
||||
'targets' => T_FALSE,
|
||||
'expectedTarget' => T_RETURN,
|
||||
],
|
||||
'Exit should be start for contents of the exit statement - close parenthesis' => [
|
||||
// Note: not sure if this is actually correct - should this be the open parenthesis ?
|
||||
'testMarker' => '/* testInsideCaseExitStatement */',
|
||||
'targets' => T_CLOSE_PARENTHESIS,
|
||||
'expectedTarget' => T_EXIT,
|
||||
],
|
||||
'Throw should be start for contents of the throw statement - new keyword' => [
|
||||
'testMarker' => '/* testInsideCaseThrowStatement */',
|
||||
'targets' => T_NEW,
|
||||
'expectedTarget' => T_THROW,
|
||||
],
|
||||
'Throw should be start for contents of the throw statement - exception name' => [
|
||||
'testMarker' => '/* testInsideCaseThrowStatement */',
|
||||
'targets' => T_STRING,
|
||||
'expectedTarget' => T_THROW,
|
||||
],
|
||||
'Throw should be start for contents of the throw statement - close parenthesis' => [
|
||||
'testMarker' => '/* testInsideCaseThrowStatement */',
|
||||
'targets' => T_CLOSE_PARENTHESIS,
|
||||
'expectedTarget' => T_THROW,
|
||||
],
|
||||
'Default keyword should be start of default statement - default itself' => [
|
||||
'testMarker' => '/* testDefaultStatement */',
|
||||
'targets' => T_DEFAULT,
|
||||
'expectedTarget' => T_DEFAULT,
|
||||
],
|
||||
'Return should be start for contents of the return statement (inside default) - variable' => [
|
||||
'testMarker' => '/* testInsideDefaultContinueStatement */',
|
||||
'targets' => T_VARIABLE,
|
||||
'expectedTarget' => T_CONTINUE,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataFindStartInsideSwitchCaseDefaultStatements()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-58
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testNotAClass */
|
||||
interface NotAClass {}
|
||||
|
||||
/* testAnonClass */
|
||||
$anon = new class() {};
|
||||
|
||||
/* testEnum */
|
||||
enum NotAClassEither {}
|
||||
|
||||
/* testClassWithoutProperties */
|
||||
class ClassWithoutProperties {}
|
||||
|
||||
/* testAbstractClass */
|
||||
abstract class AbstractClass {}
|
||||
|
||||
/* testFinalClass */
|
||||
final class FinalClass {}
|
||||
|
||||
/* testReadonlyClass */
|
||||
readonly class ReadOnlyClass {}
|
||||
|
||||
/* testFinalReadonlyClass */
|
||||
final readonly class FinalReadOnlyClass extends Foo {}
|
||||
|
||||
/* testReadonlyFinalClass */
|
||||
readonly /*comment*/ final class ReadOnlyFinalClass {}
|
||||
|
||||
/* testAbstractReadonlyClass */
|
||||
abstract readonly class AbstractReadOnlyClass {}
|
||||
|
||||
/* testReadonlyAbstractClass */
|
||||
readonly
|
||||
abstract
|
||||
class ReadOnlyAbstractClass {}
|
||||
|
||||
/* testWithCommentsAndNewLines */
|
||||
abstract
|
||||
/* comment */
|
||||
class ClassWithCommentsAndNewLines {}
|
||||
|
||||
/* testWithDocblockWithoutProperties */
|
||||
/**
|
||||
* Class docblock.
|
||||
*
|
||||
* @package SomePackage
|
||||
*
|
||||
* @phpcs:disable Standard.Cat.SniffName -- Just because.
|
||||
*/
|
||||
class ClassWithDocblock {}
|
||||
|
||||
/* testParseErrorAbstractFinal */
|
||||
final /* comment */
|
||||
|
||||
abstract // Intentional parse error, class cannot both be final and abstract.
|
||||
|
||||
class AbstractFinal {}
|
||||
Vendored
-192
@@ -1,192 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getClassProperties method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getClassProperties method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getClassProperties
|
||||
*/
|
||||
final class GetClassPropertiesTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving an expected exception when a non class token is passed.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $tokenType The type of token to look for after the marker.
|
||||
*
|
||||
* @dataProvider dataNotAClassException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNotAClassException($testMarker, $tokenType)
|
||||
{
|
||||
$this->expectRunTimeException('$stackPtr must be of type T_CLASS');
|
||||
|
||||
$target = $this->getTargetToken($testMarker, $tokenType);
|
||||
self::$phpcsFile->getClassProperties($target);
|
||||
|
||||
}//end testNotAClassException()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testNotAClassException() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|int>>
|
||||
*/
|
||||
public static function dataNotAClassException()
|
||||
{
|
||||
return [
|
||||
'interface' => [
|
||||
'testMarker' => '/* testNotAClass */',
|
||||
'tokenType' => T_INTERFACE,
|
||||
],
|
||||
'anon-class' => [
|
||||
'testMarker' => '/* testAnonClass */',
|
||||
'tokenType' => T_ANON_CLASS,
|
||||
],
|
||||
'enum' => [
|
||||
'testMarker' => '/* testEnum */',
|
||||
'tokenType' => T_ENUM,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataNotAClassException()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the properties for a class declaration.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param array<string, bool> $expected Expected function output.
|
||||
*
|
||||
* @dataProvider dataGetClassProperties
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetClassProperties($testMarker, $expected)
|
||||
{
|
||||
$class = $this->getTargetToken($testMarker, T_CLASS);
|
||||
$result = self::$phpcsFile->getClassProperties($class);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testGetClassProperties()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testGetClassProperties() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|array<string, bool|int>>>
|
||||
*/
|
||||
public static function dataGetClassProperties()
|
||||
{
|
||||
return [
|
||||
'no-properties' => [
|
||||
'testMarker' => '/* testClassWithoutProperties */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => false,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
'abstract' => [
|
||||
'testMarker' => '/* testAbstractClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => true,
|
||||
'is_final' => false,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
'final' => [
|
||||
'testMarker' => '/* testFinalClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => true,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
'readonly' => [
|
||||
'testMarker' => '/* testReadonlyClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => false,
|
||||
'is_readonly' => true,
|
||||
],
|
||||
],
|
||||
'final-readonly' => [
|
||||
'testMarker' => '/* testFinalReadonlyClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => true,
|
||||
'is_readonly' => true,
|
||||
],
|
||||
],
|
||||
'readonly-final' => [
|
||||
'testMarker' => '/* testReadonlyFinalClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => true,
|
||||
'is_readonly' => true,
|
||||
],
|
||||
],
|
||||
'abstract-readonly' => [
|
||||
'testMarker' => '/* testAbstractReadonlyClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => true,
|
||||
'is_final' => false,
|
||||
'is_readonly' => true,
|
||||
],
|
||||
],
|
||||
'readonly-abstract' => [
|
||||
'testMarker' => '/* testReadonlyAbstractClass */',
|
||||
'expected' => [
|
||||
'is_abstract' => true,
|
||||
'is_final' => false,
|
||||
'is_readonly' => true,
|
||||
],
|
||||
],
|
||||
'comments-and-new-lines' => [
|
||||
'testMarker' => '/* testWithCommentsAndNewLines */',
|
||||
'expected' => [
|
||||
'is_abstract' => true,
|
||||
'is_final' => false,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
'no-properties-with-docblock' => [
|
||||
'testMarker' => '/* testWithDocblockWithoutProperties */',
|
||||
'expected' => [
|
||||
'is_abstract' => false,
|
||||
'is_final' => false,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
'abstract-final-parse-error' => [
|
||||
'testMarker' => '/* testParseErrorAbstractFinal */',
|
||||
'expected' => [
|
||||
'is_abstract' => true,
|
||||
'is_final' => true,
|
||||
'is_readonly' => false,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetClassProperties()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-91
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testStartPoint */
|
||||
/* condition 0: namespace */
|
||||
namespace Conditions\HorribleCode {
|
||||
|
||||
/* condition 1: if */
|
||||
if (!function_exists('letsGetSerious') ) {
|
||||
|
||||
/* condition 2: function */
|
||||
function letsGetSerious() {
|
||||
|
||||
/* condition 3-1: if */
|
||||
if (isset($loadthis)) {
|
||||
doing_something();
|
||||
/* condition 3-2: else */
|
||||
} else {
|
||||
|
||||
/* condition 4: if */
|
||||
if (!class_exists('SeriouslyNestedClass')) {
|
||||
|
||||
/* condition 5: nested class */
|
||||
class SeriouslyNestedClass extends SomeOtherClass {
|
||||
|
||||
/* condition 6: class method */
|
||||
public function SeriouslyNestedMethod(/* testSeriouslyNestedMethod */ $param) {
|
||||
|
||||
/* condition 7: switch */
|
||||
switch ($param) {
|
||||
|
||||
/* condition 8a: case */
|
||||
case 'testing':
|
||||
|
||||
/* condition 9: while */
|
||||
while ($a < 10 ) {
|
||||
|
||||
/* condition 10-1: if */
|
||||
if ($a === $b) {
|
||||
|
||||
/* condition 11-1: nested anonymous class */
|
||||
return new class() {
|
||||
|
||||
/* condition 12: nested anonymous class method */
|
||||
private function DidSomeoneSayNesting() {
|
||||
|
||||
/* condition 13: closure */
|
||||
$c = function() {
|
||||
/* testDeepestNested */
|
||||
return 'closure';
|
||||
};
|
||||
}
|
||||
};
|
||||
/* condition 10-2: elseif */
|
||||
} elseif($bool) {
|
||||
echo 'hello world';
|
||||
}
|
||||
|
||||
/* condition 10-3: foreach */
|
||||
foreach ($array as $k => $v) {
|
||||
|
||||
/* condition 11-2: try */
|
||||
try {
|
||||
--$k;
|
||||
|
||||
/* condition 11-3: catch */
|
||||
} catch (Exception $e) {
|
||||
/* testInException */
|
||||
echo 'oh darn';
|
||||
/* condition 11-4: finally */
|
||||
} finally {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$a++;
|
||||
}
|
||||
break;
|
||||
|
||||
/* condition 8b: default */
|
||||
default:
|
||||
/* testInDefault */
|
||||
$return = 'nada';
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
-494
@@ -1,494 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getCondition and \PHP_CodeSniffer\Files\File:hasCondition methods.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
use PHP_CodeSniffer\Util\Tokens;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getCondition and \PHP_CodeSniffer\Files\File:hasCondition methods.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getCondition
|
||||
* @covers \PHP_CodeSniffer\Files\File::hasCondition
|
||||
*/
|
||||
final class GetConditionTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
/**
|
||||
* List of all the test markers with their target token in the test case file.
|
||||
*
|
||||
* - The startPoint token is left out as it is tested separately.
|
||||
* - The key is the type of token to look for after the test marker.
|
||||
*
|
||||
* @var array<int|string, string>
|
||||
*/
|
||||
protected static $testTargets = [
|
||||
T_VARIABLE => '/* testSeriouslyNestedMethod */',
|
||||
T_RETURN => '/* testDeepestNested */',
|
||||
T_ECHO => '/* testInException */',
|
||||
T_CONSTANT_ENCAPSED_STRING => '/* testInDefault */',
|
||||
];
|
||||
|
||||
/**
|
||||
* List of all the condition markers in the test case file.
|
||||
*
|
||||
* @var array<string>
|
||||
*/
|
||||
protected $conditionMarkers = [
|
||||
'/* condition 0: namespace */',
|
||||
'/* condition 1: if */',
|
||||
'/* condition 2: function */',
|
||||
'/* condition 3-1: if */',
|
||||
'/* condition 3-2: else */',
|
||||
'/* condition 4: if */',
|
||||
'/* condition 5: nested class */',
|
||||
'/* condition 6: class method */',
|
||||
'/* condition 7: switch */',
|
||||
'/* condition 8a: case */',
|
||||
'/* condition 9: while */',
|
||||
'/* condition 10-1: if */',
|
||||
'/* condition 11-1: nested anonymous class */',
|
||||
'/* condition 12: nested anonymous class method */',
|
||||
'/* condition 13: closure */',
|
||||
'/* condition 10-2: elseif */',
|
||||
'/* condition 10-3: foreach */',
|
||||
'/* condition 11-2: try */',
|
||||
'/* condition 11-3: catch */',
|
||||
'/* condition 11-4: finally */',
|
||||
'/* condition 8b: default */',
|
||||
];
|
||||
|
||||
/**
|
||||
* Base array with all the scope opening tokens.
|
||||
*
|
||||
* This array is merged with expected result arrays for various unit tests
|
||||
* to make sure all possible conditions are tested.
|
||||
*
|
||||
* This array should be kept in sync with the Tokens::$scopeOpeners array.
|
||||
* This array isn't auto-generated based on the array in Tokens as for these
|
||||
* tests we want to have access to the token constant names, not just their values.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
protected $conditionDefaults = [
|
||||
'T_CLASS' => false,
|
||||
'T_ANON_CLASS' => false,
|
||||
'T_INTERFACE' => false,
|
||||
'T_TRAIT' => false,
|
||||
'T_NAMESPACE' => false,
|
||||
'T_FUNCTION' => false,
|
||||
'T_CLOSURE' => false,
|
||||
'T_IF' => false,
|
||||
'T_SWITCH' => false,
|
||||
'T_CASE' => false,
|
||||
'T_DECLARE' => false,
|
||||
'T_DEFAULT' => false,
|
||||
'T_WHILE' => false,
|
||||
'T_ELSE' => false,
|
||||
'T_ELSEIF' => false,
|
||||
'T_FOR' => false,
|
||||
'T_FOREACH' => false,
|
||||
'T_DO' => false,
|
||||
'T_TRY' => false,
|
||||
'T_CATCH' => false,
|
||||
'T_FINALLY' => false,
|
||||
'T_PROPERTY' => false,
|
||||
'T_OBJECT' => false,
|
||||
'T_USE' => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* Cache for the test token stack pointers.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
protected static $testTokens = [];
|
||||
|
||||
/**
|
||||
* Cache for the marker token stack pointers.
|
||||
*
|
||||
* @var array<string, int>
|
||||
*/
|
||||
protected static $markerTokens = [];
|
||||
|
||||
|
||||
/**
|
||||
* Set up the token position caches for the tests.
|
||||
*
|
||||
* Retrieves the test tokens and marker token stack pointer positions
|
||||
* only once and caches them as they won't change between the tests anyway.
|
||||
*
|
||||
* @before
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUpCaches()
|
||||
{
|
||||
if (empty(self::$testTokens) === true) {
|
||||
foreach (self::$testTargets as $targetToken => $marker) {
|
||||
self::$testTokens[$marker] = $this->getTargetToken($marker, $targetToken);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty(self::$markerTokens) === true) {
|
||||
foreach ($this->conditionMarkers as $marker) {
|
||||
self::$markerTokens[$marker] = $this->getTargetToken($marker, Tokens::$scopeOpeners);
|
||||
}
|
||||
}
|
||||
|
||||
}//end setUpCaches()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a non-existent token pointer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonExistentToken()
|
||||
{
|
||||
$result = self::$phpcsFile->getCondition(100000, Tokens::$ooScopeTokens);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$result = self::$phpcsFile->hasCondition(100000, T_IF);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNonExistentToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a non conditional token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonConditionalToken()
|
||||
{
|
||||
$targetType = T_STRING;
|
||||
$stackPtr = $this->getTargetToken('/* testStartPoint */', $targetType);
|
||||
|
||||
$result = self::$phpcsFile->getCondition($stackPtr, T_IF);
|
||||
$this->assertFalse($result);
|
||||
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
|
||||
$this->assertFalse($result);
|
||||
|
||||
}//end testNonConditionalToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving a specific condition from a tokens "conditions" array.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param array<string, string> $expectedResults Array with the condition token type to search for as key
|
||||
* and the marker for the expected stack pointer result as a value.
|
||||
*
|
||||
* @dataProvider dataGetCondition
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetCondition($testMarker, $expectedResults)
|
||||
{
|
||||
$stackPtr = self::$testTokens[$testMarker];
|
||||
|
||||
// Add expected results for all test markers not listed in the data provider.
|
||||
$expectedResults += $this->conditionDefaults;
|
||||
|
||||
foreach ($expectedResults as $conditionType => $expected) {
|
||||
if (is_string($expected) === true) {
|
||||
$expected = self::$markerTokens[$expected];
|
||||
}
|
||||
|
||||
$result = self::$phpcsFile->getCondition($stackPtr, constant($conditionType));
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$result,
|
||||
"Assertion failed for test marker '{$testMarker}' with condition {$conditionType}"
|
||||
);
|
||||
}
|
||||
|
||||
}//end testGetCondition()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* Only the conditions which are expected to be *found* need to be listed here.
|
||||
* All other potential conditions will automatically also be tested and will expect
|
||||
* `false` as a result.
|
||||
*
|
||||
* @see testGetCondition() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|array<string, string>>>
|
||||
*/
|
||||
public static function dataGetCondition()
|
||||
{
|
||||
return [
|
||||
'testSeriouslyNestedMethod' => [
|
||||
'testMarker' => '/* testSeriouslyNestedMethod */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => '/* condition 5: nested class */',
|
||||
'T_NAMESPACE' => '/* condition 0: namespace */',
|
||||
'T_FUNCTION' => '/* condition 2: function */',
|
||||
'T_IF' => '/* condition 1: if */',
|
||||
'T_ELSE' => '/* condition 3-2: else */',
|
||||
],
|
||||
],
|
||||
'testDeepestNested' => [
|
||||
'testMarker' => '/* testDeepestNested */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => '/* condition 5: nested class */',
|
||||
'T_ANON_CLASS' => '/* condition 11-1: nested anonymous class */',
|
||||
'T_NAMESPACE' => '/* condition 0: namespace */',
|
||||
'T_FUNCTION' => '/* condition 2: function */',
|
||||
'T_CLOSURE' => '/* condition 13: closure */',
|
||||
'T_IF' => '/* condition 1: if */',
|
||||
'T_SWITCH' => '/* condition 7: switch */',
|
||||
'T_CASE' => '/* condition 8a: case */',
|
||||
'T_WHILE' => '/* condition 9: while */',
|
||||
'T_ELSE' => '/* condition 3-2: else */',
|
||||
],
|
||||
],
|
||||
'testInException' => [
|
||||
'testMarker' => '/* testInException */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => '/* condition 5: nested class */',
|
||||
'T_NAMESPACE' => '/* condition 0: namespace */',
|
||||
'T_FUNCTION' => '/* condition 2: function */',
|
||||
'T_IF' => '/* condition 1: if */',
|
||||
'T_SWITCH' => '/* condition 7: switch */',
|
||||
'T_CASE' => '/* condition 8a: case */',
|
||||
'T_WHILE' => '/* condition 9: while */',
|
||||
'T_ELSE' => '/* condition 3-2: else */',
|
||||
'T_FOREACH' => '/* condition 10-3: foreach */',
|
||||
'T_CATCH' => '/* condition 11-3: catch */',
|
||||
],
|
||||
],
|
||||
'testInDefault' => [
|
||||
'testMarker' => '/* testInDefault */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => '/* condition 5: nested class */',
|
||||
'T_NAMESPACE' => '/* condition 0: namespace */',
|
||||
'T_FUNCTION' => '/* condition 2: function */',
|
||||
'T_IF' => '/* condition 1: if */',
|
||||
'T_SWITCH' => '/* condition 7: switch */',
|
||||
'T_DEFAULT' => '/* condition 8b: default */',
|
||||
'T_ELSE' => '/* condition 3-2: else */',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetCondition()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving a specific condition from a tokens "conditions" array.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param array<string, string> $expectedResults Array with the condition token type to search for as key
|
||||
* and the marker for the expected stack pointer result as a value.
|
||||
*
|
||||
* @dataProvider dataGetConditionReversed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetConditionReversed($testMarker, $expectedResults)
|
||||
{
|
||||
$stackPtr = self::$testTokens[$testMarker];
|
||||
|
||||
// Add expected results for all test markers not listed in the data provider.
|
||||
$expectedResults += $this->conditionDefaults;
|
||||
|
||||
foreach ($expectedResults as $conditionType => $expected) {
|
||||
if (is_string($expected) === true) {
|
||||
$expected = self::$markerTokens[$expected];
|
||||
}
|
||||
|
||||
$result = self::$phpcsFile->getCondition($stackPtr, constant($conditionType), false);
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$result,
|
||||
"Assertion failed for test marker '{$testMarker}' with condition {$conditionType} (reversed)"
|
||||
);
|
||||
}
|
||||
|
||||
}//end testGetConditionReversed()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* Only the conditions which are expected to be *found* need to be listed here.
|
||||
* All other potential conditions will automatically also be tested and will expect
|
||||
* `false` as a result.
|
||||
*
|
||||
* @see testGetConditionReversed() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|array<string, string>>>
|
||||
*/
|
||||
public static function dataGetConditionReversed()
|
||||
{
|
||||
$data = self::dataGetCondition();
|
||||
|
||||
// Set up the data for the reversed results.
|
||||
$data['testSeriouslyNestedMethod']['expectedResults']['T_IF'] = '/* condition 4: if */';
|
||||
|
||||
$data['testDeepestNested']['expectedResults']['T_FUNCTION'] = '/* condition 12: nested anonymous class method */';
|
||||
$data['testDeepestNested']['expectedResults']['T_IF'] = '/* condition 10-1: if */';
|
||||
|
||||
$data['testInException']['expectedResults']['T_FUNCTION'] = '/* condition 6: class method */';
|
||||
$data['testInException']['expectedResults']['T_IF'] = '/* condition 4: if */';
|
||||
|
||||
$data['testInDefault']['expectedResults']['T_FUNCTION'] = '/* condition 6: class method */';
|
||||
$data['testInDefault']['expectedResults']['T_IF'] = '/* condition 4: if */';
|
||||
|
||||
return $data;
|
||||
|
||||
}//end dataGetConditionReversed()
|
||||
|
||||
|
||||
/**
|
||||
* Test whether a token has a condition of a certain type.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param array<string, bool> $expectedResults Array with the condition token type to search for as key
|
||||
* and the expected result as a value.
|
||||
*
|
||||
* @dataProvider dataHasCondition
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHasCondition($testMarker, $expectedResults)
|
||||
{
|
||||
$stackPtr = self::$testTokens[$testMarker];
|
||||
|
||||
// Add expected results for all test markers not listed in the data provider.
|
||||
$expectedResults += $this->conditionDefaults;
|
||||
|
||||
foreach ($expectedResults as $conditionType => $expected) {
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, constant($conditionType));
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$result,
|
||||
"Assertion failed for test marker '{$testMarker}' with condition {$conditionType}"
|
||||
);
|
||||
}
|
||||
|
||||
}//end testHasCondition()
|
||||
|
||||
|
||||
/**
|
||||
* Data Provider.
|
||||
*
|
||||
* Only list the "true" conditions in the $results array.
|
||||
* All other potential conditions will automatically also be tested
|
||||
* and will expect "false" as a result.
|
||||
*
|
||||
* @see testHasCondition() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|array<string, bool>>>
|
||||
*/
|
||||
public static function dataHasCondition()
|
||||
{
|
||||
return [
|
||||
'testSeriouslyNestedMethod' => [
|
||||
'testMarker' => '/* testSeriouslyNestedMethod */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => true,
|
||||
'T_NAMESPACE' => true,
|
||||
'T_FUNCTION' => true,
|
||||
'T_IF' => true,
|
||||
'T_ELSE' => true,
|
||||
],
|
||||
],
|
||||
'testDeepestNested' => [
|
||||
'testMarker' => '/* testDeepestNested */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => true,
|
||||
'T_ANON_CLASS' => true,
|
||||
'T_NAMESPACE' => true,
|
||||
'T_FUNCTION' => true,
|
||||
'T_CLOSURE' => true,
|
||||
'T_IF' => true,
|
||||
'T_SWITCH' => true,
|
||||
'T_CASE' => true,
|
||||
'T_WHILE' => true,
|
||||
'T_ELSE' => true,
|
||||
],
|
||||
],
|
||||
'testInException' => [
|
||||
'testMarker' => '/* testInException */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => true,
|
||||
'T_NAMESPACE' => true,
|
||||
'T_FUNCTION' => true,
|
||||
'T_IF' => true,
|
||||
'T_SWITCH' => true,
|
||||
'T_CASE' => true,
|
||||
'T_WHILE' => true,
|
||||
'T_ELSE' => true,
|
||||
'T_FOREACH' => true,
|
||||
'T_CATCH' => true,
|
||||
],
|
||||
],
|
||||
'testInDefault' => [
|
||||
'testMarker' => '/* testInDefault */',
|
||||
'expectedResults' => [
|
||||
'T_CLASS' => true,
|
||||
'T_NAMESPACE' => true,
|
||||
'T_FUNCTION' => true,
|
||||
'T_IF' => true,
|
||||
'T_SWITCH' => true,
|
||||
'T_DEFAULT' => true,
|
||||
'T_ELSE' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataHasCondition()
|
||||
|
||||
|
||||
/**
|
||||
* Test whether a token has a condition of a certain type, with multiple allowed possibilities.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHasConditionMultipleTypes()
|
||||
{
|
||||
$stackPtr = self::$testTokens['/* testInException */'];
|
||||
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, [T_TRY, T_FINALLY]);
|
||||
$this->assertFalse(
|
||||
$result,
|
||||
'Failed asserting that "testInException" does not have a "try" nor a "finally" condition'
|
||||
);
|
||||
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, [T_TRY, T_CATCH, T_FINALLY]);
|
||||
$this->assertTrue(
|
||||
$result,
|
||||
'Failed asserting that "testInException" has a "try", "catch" or "finally" condition'
|
||||
);
|
||||
|
||||
$stackPtr = self::$testTokens['/* testSeriouslyNestedMethod */'];
|
||||
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, [T_ANON_CLASS, T_CLOSURE]);
|
||||
$this->assertFalse(
|
||||
$result,
|
||||
'Failed asserting that "testSeriouslyNestedMethod" does not have an anonymous class nor a closure condition'
|
||||
);
|
||||
|
||||
$result = self::$phpcsFile->hasCondition($stackPtr, Tokens::$ooScopeTokens);
|
||||
$this->assertTrue(
|
||||
$result,
|
||||
'Failed asserting that "testSeriouslyNestedMethod" has an OO Scope token condition'
|
||||
);
|
||||
|
||||
}//end testHasConditionMultipleTypes()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-23
@@ -1,23 +0,0 @@
|
||||
/* testInvalidTokenPassed */
|
||||
print something;
|
||||
|
||||
var object =
|
||||
{
|
||||
/* testClosure */
|
||||
propertyName: function () {}
|
||||
}
|
||||
|
||||
/* testFunction */
|
||||
function functionName() {}
|
||||
|
||||
/* testClass */
|
||||
class ClassName
|
||||
{
|
||||
/* testMethod */
|
||||
methodName() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/* testFunctionUnicode */
|
||||
function π() {}
|
||||
Vendored
-158
@@ -1,158 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getDeclarationName method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getDeclarationName
|
||||
*/
|
||||
final class GetDeclarationNameJSTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
/**
|
||||
* The file extension of the test case file (without leading dot).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $fileExtension = 'js';
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving an expected exception when a non-supported token is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidTokenPassed()
|
||||
{
|
||||
$this->expectRunTimeException('Token type "T_STRING" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
|
||||
|
||||
$target = $this->getTargetToken('/* testInvalidTokenPassed */', T_STRING);
|
||||
self::$phpcsFile->getDeclarationName($target);
|
||||
|
||||
}//end testInvalidTokenPassed()
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving "null" when passed an anonymous construct or in case of a parse error.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $targetType Token type of the token to get as stackPtr.
|
||||
*
|
||||
* @dataProvider dataGetDeclarationNameNull
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDeclarationNameNull($testMarker, $targetType)
|
||||
{
|
||||
$target = $this->getTargetToken($testMarker, $targetType);
|
||||
$result = self::$phpcsFile->getDeclarationName($target);
|
||||
$this->assertNull($result);
|
||||
|
||||
}//end testGetDeclarationNameNull()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see GetDeclarationNameTest::testGetDeclarationNameNull()
|
||||
*
|
||||
* @return array<string, array<string, int|string>>
|
||||
*/
|
||||
public static function dataGetDeclarationNameNull()
|
||||
{
|
||||
return [
|
||||
'closure' => [
|
||||
'testMarker' => '/* testClosure */',
|
||||
'targetType' => T_CLOSURE,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetDeclarationNameNull()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the name of a function or OO structure.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param string $expected Expected function output.
|
||||
* @param array<int|string>|null $targetType Token type of the token to get as stackPtr.
|
||||
*
|
||||
* @dataProvider dataGetDeclarationName
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDeclarationName($testMarker, $expected, $targetType=null)
|
||||
{
|
||||
if (isset($targetType) === false) {
|
||||
$targetType = [
|
||||
T_CLASS,
|
||||
T_INTERFACE,
|
||||
T_TRAIT,
|
||||
T_ENUM,
|
||||
T_FUNCTION,
|
||||
];
|
||||
}
|
||||
|
||||
$target = $this->getTargetToken($testMarker, $targetType);
|
||||
$result = self::$phpcsFile->getDeclarationName($target);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testGetDeclarationName()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see GetDeclarationNameTest::testGetDeclarationName()
|
||||
*
|
||||
* @return array<string, array<string, string|array<int|string>>>
|
||||
*/
|
||||
public static function dataGetDeclarationName()
|
||||
{
|
||||
return [
|
||||
'function' => [
|
||||
'testMarker' => '/* testFunction */',
|
||||
'expected' => 'functionName',
|
||||
],
|
||||
'class' => [
|
||||
'testMarker' => '/* testClass */',
|
||||
'expected' => 'ClassName',
|
||||
'targetType' => [
|
||||
T_CLASS,
|
||||
T_STRING,
|
||||
],
|
||||
],
|
||||
'function-unicode-name' => [
|
||||
'testMarker' => '/* testFunctionUnicode */',
|
||||
'expected' => 'π',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetDeclarationName()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the name of JS ES6 class method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDeclarationNameES6Method()
|
||||
{
|
||||
$target = $this->getTargetToken('/* testMethod */', [T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION]);
|
||||
$result = self::$phpcsFile->getDeclarationName($target);
|
||||
$this->assertSame('methodName', $result);
|
||||
|
||||
}//end testGetDeclarationNameES6Method()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-102
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testInvalidTokenPassed */
|
||||
echo MY_CONSTANT;
|
||||
|
||||
/* testClosure */
|
||||
$closure = function() {};
|
||||
|
||||
/* testAnonClassWithParens */
|
||||
$anonClass = new class() {};
|
||||
|
||||
/* testAnonClassWithParens2 */
|
||||
$class = new class() {
|
||||
private $property = 'test';
|
||||
public function test() {}
|
||||
};
|
||||
|
||||
/* testAnonClassWithoutParens */
|
||||
$anonClass = new class {};
|
||||
|
||||
/* testAnonClassExtendsWithoutParens */
|
||||
$class = new class extends SomeClass {
|
||||
private $property = 'test';
|
||||
public function test() {}
|
||||
};
|
||||
|
||||
/* testFunction */
|
||||
function functionName() {}
|
||||
|
||||
/* testFunctionReturnByRef */
|
||||
function & functionNameByRef() {}
|
||||
|
||||
/* testClass */
|
||||
abstract class ClassName {
|
||||
/* testMethod */
|
||||
public function methodName() {}
|
||||
|
||||
/* testAbstractMethod */
|
||||
abstract protected function abstractMethodName();
|
||||
|
||||
/* testMethodReturnByRef */
|
||||
private function &MethodNameByRef();
|
||||
}
|
||||
|
||||
/* testExtendedClass */
|
||||
class ExtendedClass extends Foo {}
|
||||
|
||||
/* testInterface */
|
||||
interface InterfaceName {}
|
||||
|
||||
/* testTrait */
|
||||
trait TraitName {
|
||||
/* testFunctionEndingWithNumber */
|
||||
function ValidNameEndingWithNumber5(){}
|
||||
}
|
||||
|
||||
/* testClassWithNumber */
|
||||
class ClassWith1Number implements SomeInterface {}
|
||||
|
||||
/* testInterfaceWithNumbers */
|
||||
interface InterfaceWith12345Numbers extends AnotherInterface {}
|
||||
|
||||
/* testClassWithCommentsAndNewLines */
|
||||
class /* comment */
|
||||
|
||||
// phpcs:ignore Standard.Cat.SniffName -- for reasons
|
||||
ClassWithCommentsAndNewLines {}
|
||||
|
||||
/* testFunctionFn */
|
||||
function fn() {}
|
||||
|
||||
/* testPureEnum */
|
||||
enum Foo
|
||||
{
|
||||
case SOME_CASE;
|
||||
}
|
||||
|
||||
/* testBackedEnumSpaceBetweenNameAndColon */
|
||||
enum Hoo : string
|
||||
{
|
||||
case ONE = 'one';
|
||||
case TWO = 'two';
|
||||
}
|
||||
|
||||
/* testBackedEnumNoSpaceBetweenNameAndColon */
|
||||
enum Suit: int implements Colorful, CardGame {}
|
||||
|
||||
/* testFunctionReturnByRefWithReservedKeywordEach */
|
||||
function &each() {}
|
||||
|
||||
/* testFunctionReturnByRefWithReservedKeywordParent */
|
||||
function &parent() {}
|
||||
|
||||
/* testFunctionReturnByRefWithReservedKeywordSelf */
|
||||
function &self() {}
|
||||
|
||||
/* testFunctionReturnByRefWithReservedKeywordStatic */
|
||||
function &static() {}
|
||||
|
||||
/* testLiveCoding */
|
||||
// Intentional parse error. This has to be the last test in the file.
|
||||
function // Comment.
|
||||
Vendored
-225
@@ -1,225 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getDeclarationName method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getDeclarationName
|
||||
*/
|
||||
final class GetDeclarationNameTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving an expected exception when a non-supported token is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidTokenPassed()
|
||||
{
|
||||
$this->expectRunTimeException('Token type "T_STRING" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
|
||||
|
||||
$target = $this->getTargetToken('/* testInvalidTokenPassed */', T_STRING);
|
||||
self::$phpcsFile->getDeclarationName($target);
|
||||
|
||||
}//end testInvalidTokenPassed()
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving "null" when passed an anonymous construct or in case of a parse error.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $targetType Token type of the token to get as stackPtr.
|
||||
*
|
||||
* @dataProvider dataGetDeclarationNameNull
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDeclarationNameNull($testMarker, $targetType)
|
||||
{
|
||||
$target = $this->getTargetToken($testMarker, $targetType);
|
||||
$result = self::$phpcsFile->getDeclarationName($target);
|
||||
$this->assertNull($result);
|
||||
|
||||
}//end testGetDeclarationNameNull()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testGetDeclarationNameNull() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, int|string>>
|
||||
*/
|
||||
public static function dataGetDeclarationNameNull()
|
||||
{
|
||||
return [
|
||||
'closure' => [
|
||||
'testMarker' => '/* testClosure */',
|
||||
'targetType' => T_CLOSURE,
|
||||
],
|
||||
'anon-class-with-parentheses' => [
|
||||
'testMarker' => '/* testAnonClassWithParens */',
|
||||
'targetType' => T_ANON_CLASS,
|
||||
],
|
||||
'anon-class-with-parentheses-2' => [
|
||||
'testMarker' => '/* testAnonClassWithParens2 */',
|
||||
'targetType' => T_ANON_CLASS,
|
||||
],
|
||||
'anon-class-without-parentheses' => [
|
||||
'testMarker' => '/* testAnonClassWithoutParens */',
|
||||
'targetType' => T_ANON_CLASS,
|
||||
],
|
||||
'anon-class-extends-without-parentheses' => [
|
||||
'testMarker' => '/* testAnonClassExtendsWithoutParens */',
|
||||
'targetType' => T_ANON_CLASS,
|
||||
],
|
||||
'live-coding' => [
|
||||
'testMarker' => '/* testLiveCoding */',
|
||||
'targetType' => T_FUNCTION,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetDeclarationNameNull()
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving the name of a function or OO structure.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param string $expected Expected function output.
|
||||
* @param int|string|null $targetType Token type of the token to get as stackPtr.
|
||||
*
|
||||
* @dataProvider dataGetDeclarationName
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDeclarationName($testMarker, $expected, $targetType=null)
|
||||
{
|
||||
if (isset($targetType) === false) {
|
||||
$targetType = [
|
||||
T_CLASS,
|
||||
T_INTERFACE,
|
||||
T_TRAIT,
|
||||
T_ENUM,
|
||||
T_FUNCTION,
|
||||
];
|
||||
}
|
||||
|
||||
$target = $this->getTargetToken($testMarker, $targetType);
|
||||
$result = self::$phpcsFile->getDeclarationName($target);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testGetDeclarationName()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testGetDeclarationName() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataGetDeclarationName()
|
||||
{
|
||||
return [
|
||||
'function' => [
|
||||
'testMarker' => '/* testFunction */',
|
||||
'expected' => 'functionName',
|
||||
],
|
||||
'function-return-by-reference' => [
|
||||
'testMarker' => '/* testFunctionReturnByRef */',
|
||||
'expected' => 'functionNameByRef',
|
||||
],
|
||||
'class' => [
|
||||
'testMarker' => '/* testClass */',
|
||||
'expected' => 'ClassName',
|
||||
],
|
||||
'method' => [
|
||||
'testMarker' => '/* testMethod */',
|
||||
'expected' => 'methodName',
|
||||
],
|
||||
'abstract-method' => [
|
||||
'testMarker' => '/* testAbstractMethod */',
|
||||
'expected' => 'abstractMethodName',
|
||||
],
|
||||
'method-return-by-reference' => [
|
||||
'testMarker' => '/* testMethodReturnByRef */',
|
||||
'expected' => 'MethodNameByRef',
|
||||
],
|
||||
'extended-class' => [
|
||||
'testMarker' => '/* testExtendedClass */',
|
||||
'expected' => 'ExtendedClass',
|
||||
],
|
||||
'interface' => [
|
||||
'testMarker' => '/* testInterface */',
|
||||
'expected' => 'InterfaceName',
|
||||
],
|
||||
'trait' => [
|
||||
'testMarker' => '/* testTrait */',
|
||||
'expected' => 'TraitName',
|
||||
],
|
||||
'function-name-ends-with-number' => [
|
||||
'testMarker' => '/* testFunctionEndingWithNumber */',
|
||||
'expected' => 'ValidNameEndingWithNumber5',
|
||||
],
|
||||
'class-with-numbers-in-name' => [
|
||||
'testMarker' => '/* testClassWithNumber */',
|
||||
'expected' => 'ClassWith1Number',
|
||||
],
|
||||
'interface-with-numbers-in-name' => [
|
||||
'testMarker' => '/* testInterfaceWithNumbers */',
|
||||
'expected' => 'InterfaceWith12345Numbers',
|
||||
],
|
||||
'class-with-comments-and-new-lines' => [
|
||||
'testMarker' => '/* testClassWithCommentsAndNewLines */',
|
||||
'expected' => 'ClassWithCommentsAndNewLines',
|
||||
],
|
||||
'function-named-fn' => [
|
||||
'testMarker' => '/* testFunctionFn */',
|
||||
'expected' => 'fn',
|
||||
],
|
||||
'enum-pure' => [
|
||||
'testMarker' => '/* testPureEnum */',
|
||||
'expected' => 'Foo',
|
||||
],
|
||||
'enum-backed-space-between-name-and-colon' => [
|
||||
'testMarker' => '/* testBackedEnumSpaceBetweenNameAndColon */',
|
||||
'expected' => 'Hoo',
|
||||
],
|
||||
'enum-backed-no-space-between-name-and-colon' => [
|
||||
'testMarker' => '/* testBackedEnumNoSpaceBetweenNameAndColon */',
|
||||
'expected' => 'Suit',
|
||||
],
|
||||
'function-return-by-reference-with-reserved-keyword-each' => [
|
||||
'testMarker' => '/* testFunctionReturnByRefWithReservedKeywordEach */',
|
||||
'expected' => 'each',
|
||||
],
|
||||
'function-return-by-reference-with-reserved-keyword-parent' => [
|
||||
'testMarker' => '/* testFunctionReturnByRefWithReservedKeywordParent */',
|
||||
'expected' => 'parent',
|
||||
],
|
||||
'function-return-by-reference-with-reserved-keyword-self' => [
|
||||
'testMarker' => '/* testFunctionReturnByRefWithReservedKeywordSelf */',
|
||||
'expected' => 'self',
|
||||
],
|
||||
'function-return-by-reference-with-reserved-keyword-static' => [
|
||||
'testMarker' => '/* testFunctionReturnByRefWithReservedKeywordStatic */',
|
||||
'expected' => 'static',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetDeclarationName()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-341
@@ -1,341 +0,0 @@
|
||||
<?php
|
||||
|
||||
class TestMemberProperties
|
||||
{
|
||||
/* testVar */
|
||||
var $varA = true;
|
||||
|
||||
/* testVarType */
|
||||
var ?int $varA = true;
|
||||
|
||||
/* testPublic */
|
||||
public $varB = true;
|
||||
|
||||
/* testPublicType */
|
||||
public string $varB = true;
|
||||
|
||||
/* testProtected */
|
||||
protected $varC = true;
|
||||
|
||||
/* testProtectedType */
|
||||
protected bool $varC = true;
|
||||
|
||||
/* testPrivate */
|
||||
private $varD = true;
|
||||
|
||||
/* testPrivateType */
|
||||
private array $varD = true;
|
||||
|
||||
/* testStatic */
|
||||
static $varE = true;
|
||||
|
||||
/* testStaticType */
|
||||
static ?string $varE = true;
|
||||
|
||||
/* testStaticVar */
|
||||
static var $varF = true;
|
||||
|
||||
/* testVarStatic */
|
||||
var static $varG = true;
|
||||
|
||||
/* testPublicStatic */
|
||||
public // comment
|
||||
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
static
|
||||
$varH = true;
|
||||
|
||||
/* testProtectedStatic */
|
||||
static protected $varI = true;
|
||||
|
||||
/* testPrivateStatic */
|
||||
private static $varJ = true;
|
||||
|
||||
/* testNoPrefix */
|
||||
$varK = true;
|
||||
|
||||
/* testPublicStaticWithDocblock */
|
||||
/**
|
||||
* Comment here.
|
||||
*
|
||||
* @phpcs:ignore Standard.Category.Sniff -- because
|
||||
* @var boolean
|
||||
*/
|
||||
public static $varH = true;
|
||||
|
||||
/* testProtectedStaticWithDocblock */
|
||||
/**
|
||||
* Comment here.
|
||||
*
|
||||
* @phpcs:ignore Standard.Category.Sniff -- because
|
||||
* @var boolean
|
||||
*/
|
||||
static protected $varI = true;
|
||||
|
||||
/* testPrivateStaticWithDocblock */
|
||||
/**
|
||||
* Comment here.
|
||||
*
|
||||
* @phpcs:ignore Standard.Category.Sniff -- because
|
||||
* @var boolean
|
||||
*/
|
||||
private static $varJ = true;
|
||||
|
||||
public float
|
||||
/* testGroupType 1 */
|
||||
$x,
|
||||
/* testGroupType 2 */
|
||||
$y;
|
||||
|
||||
public static ?string
|
||||
/* testGroupNullableType 1 */
|
||||
$x = null,
|
||||
/* testGroupNullableType 2 */
|
||||
$y = null;
|
||||
|
||||
protected static
|
||||
/* testGroupProtectedStatic 1 */
|
||||
$varL,
|
||||
/* testGroupProtectedStatic 2 */
|
||||
$varM,
|
||||
/* testGroupProtectedStatic 3 */
|
||||
$varN;
|
||||
|
||||
private
|
||||
/* testGroupPrivate 1 */
|
||||
$varO = true,
|
||||
/* testGroupPrivate 2 */
|
||||
$varP = array( 'a' => 'a', 'b' => 'b' ),
|
||||
/* testGroupPrivate 3 */
|
||||
$varQ = 'string',
|
||||
/* testGroupPrivate 4 */
|
||||
$varR = 123,
|
||||
/* testGroupPrivate 5 */
|
||||
$varS = ONE / self::THREE,
|
||||
/* testGroupPrivate 6 */
|
||||
$varT = [
|
||||
'a' => 'a',
|
||||
'b' => 'b'
|
||||
],
|
||||
/* testGroupPrivate 7 */
|
||||
$varU = __DIR__ . "/base";
|
||||
|
||||
|
||||
/* testMethodParam */
|
||||
public function methodName($param) {
|
||||
/* testImportedGlobal */
|
||||
global $importedGlobal = true;
|
||||
|
||||
/* testLocalVariable */
|
||||
$localVariable = true;
|
||||
}
|
||||
|
||||
/* testPropertyAfterMethod */
|
||||
private static $varV = true;
|
||||
|
||||
/* testMessyNullableType */
|
||||
public /* comment
|
||||
*/ ? //comment
|
||||
array $foo = [];
|
||||
|
||||
/* testNamespaceType */
|
||||
public \MyNamespace\MyClass $foo;
|
||||
|
||||
/* testNullableNamespaceType 1 */
|
||||
private ?ClassName $nullableClassType;
|
||||
|
||||
/* testNullableNamespaceType 2 */
|
||||
protected ?Folder\ClassName $nullableClassType2;
|
||||
|
||||
/* testMultilineNamespaceType */
|
||||
public \MyNamespace /** comment *\/ comment */
|
||||
\MyClass /* comment */
|
||||
\Foo $foo;
|
||||
|
||||
}
|
||||
|
||||
interface Base
|
||||
{
|
||||
/* testInterfaceProperty */
|
||||
protected $anonymous;
|
||||
}
|
||||
|
||||
/* testGlobalVariable */
|
||||
$globalVariable = true;
|
||||
|
||||
/* testNotAVariable */
|
||||
return;
|
||||
|
||||
$a = ( $foo == $bar ? new stdClass() :
|
||||
new class() {
|
||||
/* testNestedProperty 1 */
|
||||
public $var = true;
|
||||
|
||||
/* testNestedMethodParam 1 */
|
||||
public function something($var = false) {}
|
||||
}
|
||||
);
|
||||
|
||||
function_call( 'param', new class {
|
||||
/* testNestedProperty 2 */
|
||||
public $year = 2017;
|
||||
|
||||
/* testNestedMethodParam 2 */
|
||||
public function __construct( $open, $post_id ) {}
|
||||
}, 10, 2 );
|
||||
|
||||
class PHP8Mixed {
|
||||
/* testPHP8MixedTypeHint */
|
||||
public static miXed $mixed;
|
||||
|
||||
/* testPHP8MixedTypeHintNullable */
|
||||
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
|
||||
private ?mixed $nullableMixed;
|
||||
}
|
||||
|
||||
class NSOperatorInType {
|
||||
/* testNamespaceOperatorTypeHint */
|
||||
public ?namespace\Name $prop;
|
||||
}
|
||||
|
||||
$anon = class() {
|
||||
/* testPHP8UnionTypesSimple */
|
||||
public int|float $unionTypeSimple;
|
||||
|
||||
/* testPHP8UnionTypesTwoClasses */
|
||||
private MyClassA|\Package\MyClassB $unionTypesTwoClasses;
|
||||
|
||||
/* testPHP8UnionTypesAllBaseTypes */
|
||||
protected array|bool|int|float|NULL|object|string $unionTypesAllBaseTypes;
|
||||
|
||||
/* testPHP8UnionTypesAllPseudoTypes */
|
||||
// Intentional fatal error - mixing types which cannot be combined, but that's not the concern of the method.
|
||||
var false|mixed|self|parent|iterable|Resource $unionTypesAllPseudoTypes;
|
||||
|
||||
/* testPHP8UnionTypesIllegalTypes */
|
||||
// Intentional fatal error - types which are not allowed for properties, but that's not the concern of the method.
|
||||
// Note: static is also not allowed as a type, but using static for a property type is not supported by the tokenizer.
|
||||
public callable|void $unionTypesIllegalTypes;
|
||||
|
||||
/* testPHP8UnionTypesNullable */
|
||||
// Intentional fatal error - nullability is not allowed with union types, but that's not the concern of the method.
|
||||
public ?int|float $unionTypesNullable;
|
||||
|
||||
/* testPHP8PseudoTypeNull */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
public null $pseudoTypeNull;
|
||||
|
||||
/* testPHP8PseudoTypeFalse */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
public false $pseudoTypeFalse;
|
||||
|
||||
/* testPHP8PseudoTypeFalseAndBool */
|
||||
// Intentional fatal error - false pseudotype is not allowed in combination with bool, but that's not the concern of the method.
|
||||
public bool|FALSE $pseudoTypeFalseAndBool;
|
||||
|
||||
/* testPHP8ObjectAndClass */
|
||||
// Intentional fatal error - object is not allowed in combination with class name, but that's not the concern of the method.
|
||||
public object|ClassName $objectAndClass;
|
||||
|
||||
/* testPHP8PseudoTypeIterableAndArray */
|
||||
// Intentional fatal error - iterable pseudotype is not allowed in combination with array or Traversable, but that's not the concern of the method.
|
||||
public iterable|array|Traversable $pseudoTypeIterableAndArray;
|
||||
|
||||
/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */
|
||||
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
|
||||
public int |string| /*comment*/ INT $duplicateTypeInUnion;
|
||||
|
||||
/* testPHP81Readonly */
|
||||
public readonly int $readonly;
|
||||
|
||||
/* testPHP81ReadonlyWithNullableType */
|
||||
public readonly ?array $readonlyWithNullableType;
|
||||
|
||||
/* testPHP81ReadonlyWithUnionType */
|
||||
public readonly string|int $readonlyWithUnionType;
|
||||
|
||||
/* testPHP81ReadonlyWithUnionTypeWithNull */
|
||||
protected ReadOnly string|null $readonlyWithUnionTypeWithNull;
|
||||
|
||||
/* testPHP81OnlyReadonlyWithUnionType */
|
||||
readonly string|int $onlyReadonly;
|
||||
|
||||
/* testPHP81OnlyReadonlyWithUnionTypeMultiple */
|
||||
readonly \InterfaceA|\Sub\InterfaceB|false
|
||||
$onlyReadonly;
|
||||
|
||||
/* testPHP81ReadonlyAndStatic */
|
||||
readonly private static ?string $readonlyAndStatic;
|
||||
|
||||
/* testPHP81ReadonlyMixedCase */
|
||||
public ReadONLY static $readonlyMixedCase;
|
||||
};
|
||||
|
||||
$anon = class {
|
||||
/* testPHP8PropertySingleAttribute */
|
||||
#[PropertyWithAttribute]
|
||||
public string $foo;
|
||||
|
||||
/* testPHP8PropertyMultipleAttributes */
|
||||
#[PropertyWithAttribute(foo: 'bar'), MyAttribute]
|
||||
protected ?int|float $bar;
|
||||
|
||||
/* testPHP8PropertyMultilineAttribute */
|
||||
#[
|
||||
PropertyWithAttribute(/* comment */ 'baz')
|
||||
]
|
||||
private mixed $baz;
|
||||
};
|
||||
|
||||
enum Suit
|
||||
{
|
||||
/* testEnumProperty */
|
||||
protected $anonymous;
|
||||
}
|
||||
|
||||
enum Direction implements ArrayAccess
|
||||
{
|
||||
case Up;
|
||||
case Down;
|
||||
|
||||
/* testEnumMethodParamNotProperty */
|
||||
public function offsetGet($val) { ... }
|
||||
}
|
||||
|
||||
$anon = class() {
|
||||
/* testPHP81IntersectionTypes */
|
||||
public Foo&Bar $intersectionType;
|
||||
|
||||
/* testPHP81MoreIntersectionTypes */
|
||||
public Foo&Bar&Baz $moreIntersectionTypes;
|
||||
|
||||
/* testPHP81IllegalIntersectionTypes */
|
||||
// Intentional fatal error - types which are not allowed for intersection type, but that's not the concern of the method.
|
||||
public int&string $illegalIntersectionType;
|
||||
|
||||
/* testPHP81NullableIntersectionType */
|
||||
// Intentional fatal error - nullability is not allowed with intersection type, but that's not the concern of the method.
|
||||
public ?Foo&Bar $nullableIntersectionType;
|
||||
};
|
||||
|
||||
$anon = class() {
|
||||
/* testPHP82PseudoTypeTrue */
|
||||
public true $pseudoTypeTrue;
|
||||
|
||||
/* testPHP82NullablePseudoTypeTrue */
|
||||
static protected ?true $pseudoTypeNullableTrue;
|
||||
|
||||
/* testPHP82PseudoTypeTrueInUnion */
|
||||
private int|string|true $pseudoTypeTrueInUnion;
|
||||
|
||||
/* testPHP82PseudoTypeFalseAndTrue */
|
||||
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
|
||||
readonly true|FALSE $pseudoTypeFalseAndTrue;
|
||||
};
|
||||
|
||||
class WhitespaceAndCommentsInTypes {
|
||||
/* testUnionTypeWithWhitespaceAndComment */
|
||||
public int | /*comment*/ string $hasWhitespaceAndComment;
|
||||
|
||||
/* testIntersectionTypeWithWhitespaceAndComment */
|
||||
public \Foo /*comment*/ & Bar $hasWhitespaceAndComment;
|
||||
}
|
||||
Vendored
-1139
File diff suppressed because it is too large
Load Diff
-4
@@ -1,4 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testParseError */
|
||||
function missingOpenParens // Intentional parse error.
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
|
||||
*/
|
||||
final class GetMethodParametersParseError1Test extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving an empty array when encountering a specific parse error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseError()
|
||||
{
|
||||
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
|
||||
$result = self::$phpcsFile->getMethodParameters($target);
|
||||
|
||||
$this->assertSame([], $result);
|
||||
|
||||
}//end testParseError()
|
||||
|
||||
|
||||
}//end class
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testParseError */
|
||||
function missingCloseParens( // Intentional parse error.
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getMethodParameters method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getMethodParameters
|
||||
*/
|
||||
final class GetMethodParametersParseError2Test extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test receiving an empty array when encountering a specific parse error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseError()
|
||||
{
|
||||
$target = $this->getTargetToken('/* testParseError */', [T_FUNCTION, T_CLOSURE, T_FN]);
|
||||
$result = self::$phpcsFile->getMethodParameters($target);
|
||||
|
||||
$this->assertSame([], $result);
|
||||
|
||||
}//end testParseError()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-321
@@ -1,321 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testImportUse */
|
||||
use Vendor\Package\Sub as Alias;
|
||||
|
||||
/* testImportGroupUse */
|
||||
use Vendor\Package\Sub\{
|
||||
ClassA,
|
||||
ClassB as BAlias,
|
||||
};
|
||||
|
||||
if ($foo) {}
|
||||
|
||||
/* testTraitUse */
|
||||
class TraitUse {
|
||||
use ImportedTrait;
|
||||
|
||||
function methodName() {}
|
||||
}
|
||||
|
||||
/* testNotAFunction */
|
||||
interface NotAFunction {};
|
||||
|
||||
/* testFunctionNoParams */
|
||||
function noParams() {}
|
||||
|
||||
/* testPassByReference */
|
||||
function passByReference(&$var) {}
|
||||
|
||||
/* testArrayHint */
|
||||
function arrayHint(array $var) {}
|
||||
|
||||
/* testVariable */
|
||||
function variable($var) {}
|
||||
|
||||
/* testSingleDefaultValue */
|
||||
function defaultValue($var1=self::CONSTANT) {}
|
||||
|
||||
/* testDefaultValues */
|
||||
function defaultValues($var1=1, $var2='value') {}
|
||||
|
||||
/* testTypeHint */
|
||||
function typeHint(foo $var1, bar $var2) {}
|
||||
|
||||
class MyClass {
|
||||
/* testSelfTypeHint */
|
||||
function typeSelfHint(self $var) {}
|
||||
}
|
||||
|
||||
/* testNullableTypeHint */
|
||||
function nullableTypeHint(?int $var1, ?\bar $var2) {}
|
||||
|
||||
/* testBitwiseAndConstantExpressionDefaultValue */
|
||||
function myFunction($a = 10 & 20) {}
|
||||
|
||||
/* testArrowFunction */
|
||||
fn(int $a, ...$b) => $b;
|
||||
|
||||
/* testArrowFunctionReturnByRef */
|
||||
fn&(?string $a) => $b;
|
||||
|
||||
/* testArrayDefaultValues */
|
||||
function arrayDefaultValues($var1 = [], $var2 = array(1, 2, 3) ) {}
|
||||
|
||||
/* testConstantDefaultValueSecondParam */
|
||||
function constantDefaultValueSecondParam($var1, $var2 = M_PI) {}
|
||||
|
||||
/* testScalarTernaryExpressionInDefault */
|
||||
function ternayInDefault( $a = FOO ? 'bar' : 10, ? bool $b ) {}
|
||||
|
||||
/* testVariadicFunction */
|
||||
function variadicFunction( int ... $a ) {}
|
||||
|
||||
/* testVariadicByRefFunction */
|
||||
function variadicByRefFunction( &...$a ) {}
|
||||
|
||||
/* testVariadicFunctionClassType */
|
||||
function variableLengthArgument($unit, DateInterval ...$intervals) {}
|
||||
|
||||
/* testNameSpacedTypeDeclaration */
|
||||
function namespacedClassType( \Package\Sub\ClassName $a, ?Sub\AnotherClass $b ) {}
|
||||
|
||||
/* testWithAllTypes */
|
||||
class testAllTypes {
|
||||
function allTypes(
|
||||
?ClassName $a,
|
||||
self $b,
|
||||
parent $c,
|
||||
object $d,
|
||||
?int $e,
|
||||
string &$f,
|
||||
iterable $g,
|
||||
bool $h = true,
|
||||
callable $i = 'is_null',
|
||||
float $j = 1.1,
|
||||
array ...$k
|
||||
) {}
|
||||
}
|
||||
|
||||
/* testArrowFunctionWithAllTypes */
|
||||
$fn = fn(
|
||||
?ClassName $a,
|
||||
self $b,
|
||||
parent $c,
|
||||
object $d,
|
||||
?int $e,
|
||||
string &$f,
|
||||
iterable $g,
|
||||
bool $h = true,
|
||||
callable $i = 'is_null',
|
||||
float $j = 1.1,
|
||||
array ...$k
|
||||
) => $something;
|
||||
|
||||
/* testMessyDeclaration */
|
||||
function messyDeclaration(
|
||||
// comment
|
||||
?\MyNS /* comment */
|
||||
\ SubCat // phpcs:ignore Standard.Cat.Sniff -- for reasons.
|
||||
\ MyClass $a,
|
||||
$b /* test */ = /* test */ 'default' /* test*/,
|
||||
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
? /*comment*/
|
||||
bool // phpcs:disable Stnd.Cat.Sniff -- For reasons.
|
||||
& /*test*/ ... /* phpcs:ignore */ $c
|
||||
) {}
|
||||
|
||||
/* testPHP8MixedTypeHint */
|
||||
function mixedTypeHint(mixed &...$var1) {}
|
||||
|
||||
/* testPHP8MixedTypeHintNullable */
|
||||
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
|
||||
function mixedTypeHintNullable(?Mixed $var1) {}
|
||||
|
||||
/* testNamespaceOperatorTypeHint */
|
||||
function namespaceOperatorTypeHint(?namespace\Name $var1) {}
|
||||
|
||||
/* testPHP8UnionTypesSimple */
|
||||
function unionTypeSimple(int|float $number, self|parent &...$obj) {}
|
||||
|
||||
/* testPHP8UnionTypesWithSpreadOperatorAndReference */
|
||||
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB ) {}
|
||||
|
||||
/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
|
||||
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;
|
||||
|
||||
/* testPHP8UnionTypesTwoClasses */
|
||||
function unionTypesTwoClasses(MyClassA|\Package\MyClassB $var) {}
|
||||
|
||||
/* testPHP8UnionTypesAllBaseTypes */
|
||||
function unionTypesAllBaseTypes(array|bool|callable|int|float|null|object|string $var) {}
|
||||
|
||||
/* testPHP8UnionTypesAllPseudoTypes */
|
||||
// Intentional fatal error - mixing types which cannot be combined, but that's not the concern of the method.
|
||||
function unionTypesAllPseudoTypes(false|mixed|self|parent|iterable|Resource $var) {}
|
||||
|
||||
/* testPHP8UnionTypesNullable */
|
||||
// Intentional fatal error - nullability is not allowed with union types, but that's not the concern of the method.
|
||||
$closure = function (?int|float $number) {};
|
||||
|
||||
/* testPHP8PseudoTypeNull */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
function pseudoTypeNull(null $var = null) {}
|
||||
|
||||
/* testPHP8PseudoTypeFalse */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
function pseudoTypeFalse(false $var = false) {}
|
||||
|
||||
/* testPHP8PseudoTypeFalseAndBool */
|
||||
// Intentional fatal error - false pseudotype is not allowed in combination with bool, but that's not the concern of the method.
|
||||
function pseudoTypeFalseAndBool(bool|false $var = false) {}
|
||||
|
||||
/* testPHP8ObjectAndClass */
|
||||
// Intentional fatal error - object is not allowed in combination with class name, but that's not the concern of the method.
|
||||
function objectAndClass(object|ClassName $var) {}
|
||||
|
||||
/* testPHP8PseudoTypeIterableAndArray */
|
||||
// Intentional fatal error - iterable pseudotype is not allowed in combination with array or Traversable, but that's not the concern of the method.
|
||||
function pseudoTypeIterableAndArray(iterable|array|Traversable $var) {}
|
||||
|
||||
/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */
|
||||
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
|
||||
function duplicateTypeInUnion( int | string /*comment*/ | INT $var) {}
|
||||
|
||||
class ConstructorPropertyPromotionNoTypes {
|
||||
/* testPHP8ConstructorPropertyPromotionNoTypes */
|
||||
public function __construct(
|
||||
public $x = 0.0,
|
||||
protected $y = '',
|
||||
private $z = null,
|
||||
) {}
|
||||
}
|
||||
|
||||
class ConstructorPropertyPromotionWithTypes {
|
||||
/* testPHP8ConstructorPropertyPromotionWithTypes */
|
||||
public function __construct(protected float|int $x, public ?string &$y = 'test', private mixed $z) {}
|
||||
}
|
||||
|
||||
class ConstructorPropertyPromotionAndNormalParams {
|
||||
/* testPHP8ConstructorPropertyPromotionAndNormalParam */
|
||||
public function __construct(public int $promotedProp, ?int $normalArg) {}
|
||||
}
|
||||
|
||||
class ConstructorPropertyPromotionWithReadOnly {
|
||||
/* testPHP81ConstructorPropertyPromotionWithReadOnly */
|
||||
public function __construct(public readonly ?int $promotedProp, ReadOnly private string|bool &$promotedToo) {}
|
||||
}
|
||||
|
||||
class ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration {
|
||||
/* testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration */
|
||||
// Intentional fatal error. Readonly properties MUST be typed.
|
||||
public function __construct(public readonly $promotedProp, ReadOnly private &$promotedToo) {}
|
||||
}
|
||||
|
||||
class ConstructorPropertyPromotionWithOnlyReadOnly {
|
||||
/* testPHP81ConstructorPropertyPromotionWithOnlyReadOnly */
|
||||
public function __construct(readonly Foo&Bar $promotedProp, readonly ?bool $promotedToo,) {}
|
||||
}
|
||||
|
||||
/* testPHP8ConstructorPropertyPromotionGlobalFunction */
|
||||
// Intentional fatal error. Property promotion not allowed in non-constructor, but that's not the concern of this method.
|
||||
function globalFunction(private $x) {}
|
||||
|
||||
abstract class ConstructorPropertyPromotionAbstractMethod {
|
||||
/* testPHP8ConstructorPropertyPromotionAbstractMethod */
|
||||
// Intentional fatal error.
|
||||
// 1. Property promotion not allowed in abstract method, but that's not the concern of this method.
|
||||
// 2. Variadic arguments not allowed in property promotion, but that's not the concern of this method.
|
||||
// 3. The callable type is not supported for properties, but that's not the concern of this method.
|
||||
abstract public function __construct(public callable $y, private ...$x);
|
||||
}
|
||||
|
||||
/* testCommentsInParameter */
|
||||
function commentsInParams(
|
||||
// Leading comment.
|
||||
?MyClass /*-*/ & /*-*/.../*-*/ $param /*-*/ = /*-*/ 'default value' . /*-*/ 'second part' // Trailing comment.
|
||||
) {}
|
||||
|
||||
/* testParameterAttributesInFunctionDeclaration */
|
||||
class ParametersWithAttributes(
|
||||
public function __construct(
|
||||
#[\MyExample\MyAttribute] private string $constructorPropPromTypedParamSingleAttribute,
|
||||
#[MyAttr([1, 2])]
|
||||
Type|false
|
||||
$typedParamSingleAttribute,
|
||||
#[MyAttribute(1234), MyAttribute(5678)] ?int $nullableTypedParamMultiAttribute,
|
||||
#[WithoutArgument] #[SingleArgument(0)] $nonTypedParamTwoAttributes,
|
||||
#[MyAttribute(array("key" => "value"))]
|
||||
&...$otherParam,
|
||||
) {}
|
||||
}
|
||||
|
||||
/* testPHP8IntersectionTypes */
|
||||
function intersectionTypes(Foo&Bar $obj1, Boo&Bar $obj2) {}
|
||||
|
||||
/* testPHP81IntersectionTypesWithSpreadOperatorAndReference */
|
||||
function globalFunctionWithSpreadAndReference(Boo&Bar &$paramA, Foo&Bar ...$paramB) {}
|
||||
|
||||
/* testPHP81MoreIntersectionTypes */
|
||||
function moreIntersectionTypes(MyClassA&\Package\MyClassB&\Package\MyClassC $var) {}
|
||||
|
||||
/* testPHP81IllegalIntersectionTypes */
|
||||
// Intentional fatal error - simple types are not allowed with intersection types, but that's not the concern of the method.
|
||||
$closure = function (string&int $numeric_string) {};
|
||||
|
||||
/* testPHP81NullableIntersectionTypes */
|
||||
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
|
||||
$closure = function (?Foo&Bar $object) {};
|
||||
|
||||
/* testPHP82PseudoTypeTrue */
|
||||
function pseudoTypeTrue(?true $var = true) {}
|
||||
|
||||
/* testPHP82PseudoTypeFalseAndTrue */
|
||||
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
|
||||
function pseudoTypeFalseAndTrue(true|false $var = true) {}
|
||||
|
||||
/* testPHP81NewInInitializers */
|
||||
function newInInitializers(
|
||||
TypeA $new = new TypeA(self::CONST_VALUE),
|
||||
\Package\TypeB $newToo = new \Package\TypeB(10, 'string'),
|
||||
) {}
|
||||
|
||||
/* testFunctionCallFnPHPCS353-354 */
|
||||
$value = $obj->fn(true);
|
||||
|
||||
/* testClosureNoParams */
|
||||
function() {};
|
||||
|
||||
/* testClosure */
|
||||
function( $a = 'test' ) {};
|
||||
|
||||
/* testClosureUseNoParams */
|
||||
function() use() {};
|
||||
|
||||
/* testClosureUse */
|
||||
function() use( $foo, $bar ) {};
|
||||
|
||||
/* testFunctionParamListWithTrailingComma */
|
||||
function trailingComma(
|
||||
?string $foo /*comment*/ ,
|
||||
$bar = 0,
|
||||
) {}
|
||||
|
||||
/* testClosureParamListWithTrailingComma */
|
||||
function(
|
||||
$foo,
|
||||
$bar,
|
||||
) {};
|
||||
|
||||
/* testArrowFunctionParamListWithTrailingComma */
|
||||
$fn = fn( ?int $a , ...$b, ) => $b;
|
||||
|
||||
/* testClosureUseWithTrailingComma */
|
||||
function() use(
|
||||
$foo /*comment*/ ,
|
||||
$bar,
|
||||
) {};
|
||||
|
||||
/* testArrowFunctionLiveCoding */
|
||||
// Intentional parse error. This has to be the last test in the file.
|
||||
$fn = fn
|
||||
Vendored
-3027
File diff suppressed because it is too large
Load Diff
Vendored
-195
@@ -1,195 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testBasicFunction */
|
||||
function myFunction() {}
|
||||
|
||||
/* testReturnFunction */
|
||||
function myFunction(array ...$arrays): array
|
||||
{
|
||||
/* testNestedClosure */
|
||||
return array_map(function(array $array): int {
|
||||
return array_sum($array);
|
||||
}, $arrays);
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
/* testBasicMethod */
|
||||
function myFunction() {}
|
||||
|
||||
/* testPrivateStaticMethod */
|
||||
private static function myFunction() {}
|
||||
|
||||
/* testFinalMethod */
|
||||
final public function myFunction() {}
|
||||
|
||||
/* testProtectedReturnMethod */
|
||||
protected function myFunction() : int {}
|
||||
|
||||
/* testPublicReturnMethod */
|
||||
public function myFunction(): array {}
|
||||
|
||||
/* testNullableReturnMethod */
|
||||
public function myFunction(): ?array {}
|
||||
|
||||
/* testMessyNullableReturnMethod */
|
||||
public function myFunction() /* comment
|
||||
*/ :
|
||||
/* comment */ ? // phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
array {}
|
||||
|
||||
/* testReturnNamespace */
|
||||
function myFunction(): \MyNamespace\MyClass {}
|
||||
|
||||
/* testReturnMultilineNamespace */
|
||||
// Parse error in PHP 8.0.
|
||||
function myFunction(): \MyNamespace /** comment *\/ comment */
|
||||
\MyClass /* comment */
|
||||
\Foo {}
|
||||
|
||||
/* testReturnUnqualifiedName */
|
||||
private function myFunction(): ?MyClass {}
|
||||
|
||||
/* testReturnPartiallyQualifiedName */
|
||||
function myFunction(): Sub\Level\MyClass {}
|
||||
}
|
||||
|
||||
abstract class MyClass
|
||||
{
|
||||
/* testAbstractMethod */
|
||||
abstract function myFunction();
|
||||
|
||||
/* testAbstractReturnMethod */
|
||||
abstract protected function myFunction(): bool;
|
||||
}
|
||||
|
||||
interface MyInterface
|
||||
{
|
||||
/* testInterfaceMethod */
|
||||
function myFunction();
|
||||
}
|
||||
|
||||
$result = array_map(
|
||||
/* testArrowFunction */
|
||||
static fn(int $number) : int => $number + 1,
|
||||
$numbers
|
||||
);
|
||||
|
||||
class ReturnMe {
|
||||
/* testReturnTypeStatic */
|
||||
private function myFunction(): static {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/* testReturnTypeNullableStatic */
|
||||
function myNullableFunction(): ?static {
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/* testPHP8MixedTypeHint */
|
||||
function mixedTypeHint() :mixed {}
|
||||
|
||||
/* testPHP8MixedTypeHintNullable */
|
||||
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
|
||||
function mixedTypeHintNullable(): ?mixed {}
|
||||
|
||||
/* testNamespaceOperatorTypeHint */
|
||||
function namespaceOperatorTypeHint() : ?namespace\Name {}
|
||||
|
||||
/* testPHP8UnionTypesSimple */
|
||||
function unionTypeSimple($number) : int|float {}
|
||||
|
||||
/* testPHP8UnionTypesTwoClasses */
|
||||
$fn = fn($var): MyClassA|\Package\MyClassB => $var;
|
||||
|
||||
/* testPHP8UnionTypesAllBaseTypes */
|
||||
function unionTypesAllBaseTypes() : array|bool|callable|int|float|null|Object|string {}
|
||||
|
||||
/* testPHP8UnionTypesAllPseudoTypes */
|
||||
// Intentional fatal error - mixing types which cannot be combined, but that's not the concern of the method.
|
||||
function unionTypesAllPseudoTypes($var) : false|MIXED|self|parent|static|iterable|Resource|void {}
|
||||
|
||||
/* testPHP8UnionTypesNullable */
|
||||
// Intentional fatal error - nullability is not allowed with union types, but that's not the concern of the method.
|
||||
$closure = function () use($a) :?int|float {};
|
||||
|
||||
/* testPHP8PseudoTypeNull */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - null pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
function pseudoTypeNull(): null {}
|
||||
|
||||
/* testPHP8PseudoTypeFalse */
|
||||
// PHP 8.0 - 8.1: Intentional fatal error - false pseudotype is only allowed in union types, but that's not the concern of the method.
|
||||
function pseudoTypeFalse(): false {}
|
||||
|
||||
/* testPHP8PseudoTypeFalseAndBool */
|
||||
// Intentional fatal error - false pseudotype is not allowed in combination with bool, but that's not the concern of the method.
|
||||
function pseudoTypeFalseAndBool(): bool|false {}
|
||||
|
||||
/* testPHP8ObjectAndClass */
|
||||
// Intentional fatal error - object is not allowed in combination with class name, but that's not the concern of the method.
|
||||
function objectAndClass(): object|ClassName {}
|
||||
|
||||
/* testPHP8PseudoTypeIterableAndArray */
|
||||
// Intentional fatal error - iterable pseudotype is not allowed in combination with array or Traversable, but that's not the concern of the method.
|
||||
interface FooBar {
|
||||
public function pseudoTypeIterableAndArray(): iterable|array|Traversable;
|
||||
}
|
||||
|
||||
/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */
|
||||
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
|
||||
function duplicateTypeInUnion(): int | /*comment*/ string | INT {}
|
||||
|
||||
/* testPHP81NeverType */
|
||||
function never(): never {}
|
||||
|
||||
/* testPHP81NullableNeverType */
|
||||
// Intentional fatal error - nullability is not allowed with never, but that's not the concern of the method.
|
||||
function nullableNever(): ?never {}
|
||||
|
||||
/* testPHP8IntersectionTypes */
|
||||
function intersectionTypes(): Foo&Bar {}
|
||||
|
||||
/* testPHP81MoreIntersectionTypes */
|
||||
function moreIntersectionTypes(): MyClassA&\Package\MyClassB&\Package\MyClassC {}
|
||||
|
||||
/* testPHP81IntersectionArrowFunction */
|
||||
$fn = fn($var): MyClassA&\Package\MyClassB => $var;
|
||||
|
||||
/* testPHP81IllegalIntersectionTypes */
|
||||
// Intentional fatal error - simple types are not allowed with intersection types, but that's not the concern of the method.
|
||||
$closure = function (): string&int {};
|
||||
|
||||
/* testPHP81NullableIntersectionTypes */
|
||||
// Intentional fatal error - nullability is not allowed with intersection types, but that's not the concern of the method.
|
||||
$closure = function (): ?Foo&Bar {};
|
||||
|
||||
/* testPHP82PseudoTypeTrue */
|
||||
function pseudoTypeTrue(): ?true {}
|
||||
|
||||
/* testPHP82PseudoTypeFalseAndTrue */
|
||||
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
|
||||
function pseudoTypeFalseAndTrue(): true|false {}
|
||||
|
||||
/* testNotAFunction */
|
||||
return true;
|
||||
|
||||
/* testPhpcsIssue1264 */
|
||||
function foo() : array {
|
||||
echo $foo;
|
||||
}
|
||||
|
||||
/* testArrowFunctionArrayReturnValue */
|
||||
$fn = fn(): array => [a($a, $b)];
|
||||
|
||||
/* testArrowFunctionReturnByRef */
|
||||
fn&(?string $a) : ?string => $b;
|
||||
|
||||
/* testFunctionCallFnPHPCS353-354 */
|
||||
$value = $obj->fn(true);
|
||||
|
||||
/* testFunctionDeclarationNestedInTernaryPHPCS2975 */
|
||||
return (!$a ? [ new class { public function b(): c {} } ] : []);
|
||||
|
||||
/* testArrowFunctionLiveCoding */
|
||||
// Intentional parse error. This has to be the last test in the file.
|
||||
$fn = fn
|
||||
Vendored
-1327
File diff suppressed because it is too large
Load Diff
Vendored
-25
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testNamespace */
|
||||
namespace Foo\Bar\Baz;
|
||||
|
||||
/* testUseWithComments */
|
||||
use Foo /*comment*/ \ Bar
|
||||
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
\ Bah;
|
||||
|
||||
$cl = function() {
|
||||
/* testCalculation */
|
||||
return 1 + 2 +
|
||||
// Comment.
|
||||
3 + 4
|
||||
+ 5 + 6 + 7 > 20;
|
||||
}
|
||||
|
||||
/* testEchoWithTabs */
|
||||
echo 'foo',
|
||||
'bar' ,
|
||||
'baz';
|
||||
|
||||
/* testEndOfFile */
|
||||
echo $foo;
|
||||
Vendored
-334
@@ -1,334 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::getTokensAsString method.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022-2024 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File:getTokensAsString method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::getTokensAsString
|
||||
*/
|
||||
final class GetTokensAsStringTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a non-existent token pointer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonExistentToken()
|
||||
{
|
||||
$this->expectRunTimeException('The $start position for getTokensAsString() must exist in the token stack');
|
||||
|
||||
self::$phpcsFile->getTokensAsString(100000, 10);
|
||||
|
||||
}//end testNonExistentToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a non integer `$start`, like the result of a failed $phpcsFile->findNext().
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonIntegerStart()
|
||||
{
|
||||
$this->expectRunTimeException('The $start position for getTokensAsString() must exist in the token stack');
|
||||
|
||||
self::$phpcsFile->getTokensAsString(false, 10);
|
||||
|
||||
}//end testNonIntegerStart()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a non integer `$length`.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNonIntegerLength()
|
||||
{
|
||||
$result = self::$phpcsFile->getTokensAsString(10, false);
|
||||
$this->assertSame('', $result);
|
||||
|
||||
$result = self::$phpcsFile->getTokensAsString(10, 1.5);
|
||||
$this->assertSame('', $result);
|
||||
|
||||
}//end testNonIntegerLength()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a zero or negative `$length`.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLengthEqualToOrLessThanZero()
|
||||
{
|
||||
$result = self::$phpcsFile->getTokensAsString(10, -10);
|
||||
$this->assertSame('', $result);
|
||||
|
||||
$result = self::$phpcsFile->getTokensAsString(10, 0);
|
||||
$this->assertSame('', $result);
|
||||
|
||||
}//end testLengthEqualToOrLessThanZero()
|
||||
|
||||
|
||||
/**
|
||||
* Test passing a `$length` beyond the end of the file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLengthBeyondEndOfFile()
|
||||
{
|
||||
$semicolon = $this->getTargetToken('/* testEndOfFile */', T_SEMICOLON);
|
||||
$result = self::$phpcsFile->getTokensAsString($semicolon, 20);
|
||||
$this->assertSame(
|
||||
';
|
||||
',
|
||||
$result
|
||||
);
|
||||
|
||||
}//end testLengthBeyondEndOfFile()
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a token set as a string.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $startTokenType The type of token(s) to look for for the start of the string.
|
||||
* @param int $length Token length to get.
|
||||
* @param string $expected The expected function return value.
|
||||
*
|
||||
* @dataProvider dataGetTokensAsString()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetTokensAsString($testMarker, $startTokenType, $length, $expected)
|
||||
{
|
||||
$start = $this->getTargetToken($testMarker, $startTokenType);
|
||||
$result = self::$phpcsFile->getTokensAsString($start, $length);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testGetTokensAsString()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testGetTokensAsString() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|int>>
|
||||
*/
|
||||
public static function dataGetTokensAsString()
|
||||
{
|
||||
return [
|
||||
'length-0' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 0,
|
||||
'expected' => '',
|
||||
],
|
||||
'length-1' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 1,
|
||||
'expected' => '1',
|
||||
],
|
||||
'length-2' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 2,
|
||||
'expected' => '1 ',
|
||||
],
|
||||
'length-3' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 3,
|
||||
'expected' => '1 +',
|
||||
],
|
||||
'length-4' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 4,
|
||||
'expected' => '1 + ',
|
||||
],
|
||||
'length-5' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 5,
|
||||
'expected' => '1 + 2',
|
||||
],
|
||||
'length-6' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 6,
|
||||
'expected' => '1 + 2 ',
|
||||
],
|
||||
'length-7' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 7,
|
||||
'expected' => '1 + 2 +',
|
||||
],
|
||||
'length-8' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 8,
|
||||
'expected' => '1 + 2 +
|
||||
',
|
||||
],
|
||||
'length-9' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 9,
|
||||
'expected' => '1 + 2 +
|
||||
',
|
||||
],
|
||||
'length-10' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 10,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
',
|
||||
],
|
||||
'length-11' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 11,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
',
|
||||
],
|
||||
'length-12' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 12,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
3',
|
||||
],
|
||||
'length-13' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 13,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
3 ',
|
||||
],
|
||||
'length-14' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 14,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
3 +',
|
||||
],
|
||||
'length-34' => [
|
||||
'testMarker' => '/* testCalculation */',
|
||||
'startTokenType' => T_LNUMBER,
|
||||
'length' => 34,
|
||||
'expected' => '1 + 2 +
|
||||
// Comment.
|
||||
3 + 4
|
||||
+ 5 + 6 + 7 > 20;',
|
||||
],
|
||||
'namespace' => [
|
||||
'testMarker' => '/* testNamespace */',
|
||||
'startTokenType' => T_NAMESPACE,
|
||||
'length' => 8,
|
||||
'expected' => 'namespace Foo\Bar\Baz;',
|
||||
],
|
||||
'use-with-comments' => [
|
||||
'testMarker' => '/* testUseWithComments */',
|
||||
'startTokenType' => T_USE,
|
||||
'length' => 17,
|
||||
'expected' => 'use Foo /*comment*/ \ Bar
|
||||
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
\ Bah;',
|
||||
],
|
||||
'echo-with-tabs' => [
|
||||
'testMarker' => '/* testEchoWithTabs */',
|
||||
'startTokenType' => T_ECHO,
|
||||
'length' => 13,
|
||||
'expected' => 'echo \'foo\',
|
||||
\'bar\' ,
|
||||
\'baz\';',
|
||||
],
|
||||
'end-of-file' => [
|
||||
'testMarker' => '/* testEndOfFile */',
|
||||
'startTokenType' => T_ECHO,
|
||||
'length' => 4,
|
||||
'expected' => 'echo $foo;',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetTokensAsString()
|
||||
|
||||
|
||||
/**
|
||||
* Test getting a token set as a string with the original, non tab-replaced content.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int|string $startTokenType The type of token(s) to look for for the start of the string.
|
||||
* @param int $length Token length to get.
|
||||
* @param string $expected The expected function return value.
|
||||
*
|
||||
* @dataProvider dataGetOrigContent()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetOrigContent($testMarker, $startTokenType, $length, $expected)
|
||||
{
|
||||
$start = $this->getTargetToken($testMarker, $startTokenType);
|
||||
$result = self::$phpcsFile->getTokensAsString($start, $length, true);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testGetOrigContent()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testGetOrigContent() For the array format.
|
||||
*
|
||||
* @return array<string, array<string, string|int>>
|
||||
*/
|
||||
public static function dataGetOrigContent()
|
||||
{
|
||||
return [
|
||||
'use-with-comments' => [
|
||||
'testMarker' => '/* testUseWithComments */',
|
||||
'startTokenType' => T_USE,
|
||||
'length' => 17,
|
||||
'expected' => 'use Foo /*comment*/ \ Bar
|
||||
// phpcs:ignore Stnd.Cat.Sniff -- For reasons.
|
||||
\ Bah;',
|
||||
],
|
||||
'echo-with-tabs' => [
|
||||
'testMarker' => '/* testEchoWithTabs */',
|
||||
'startTokenType' => T_ECHO,
|
||||
'length' => 13,
|
||||
'expected' => 'echo \'foo\',
|
||||
\'bar\' ,
|
||||
\'baz\';',
|
||||
],
|
||||
'end-of-file' => [
|
||||
'testMarker' => '/* testEndOfFile */',
|
||||
'startTokenType' => T_ECHO,
|
||||
'length' => 4,
|
||||
'expected' => 'echo $foo;',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataGetOrigContent()
|
||||
|
||||
|
||||
}//end class
|
||||
-210
@@ -1,210 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testTokenizerIssue1971PHPCSlt330gt271A */
|
||||
// This has to be the first test in the file!
|
||||
[&$a, [$b, /* testTokenizerIssue1971PHPCSlt330gt271B */ &$c]] = $array;
|
||||
|
||||
/* testBitwiseAndA */
|
||||
error_reporting( E_NOTICE & E_STRICT );
|
||||
|
||||
/* testBitwiseAndB */
|
||||
$a = [ $something & $somethingElse ];
|
||||
|
||||
/* testBitwiseAndC */
|
||||
$a = [ $first, $something & self::$somethingElse ];
|
||||
|
||||
/* testBitwiseAndD */
|
||||
$a = array( $first, $something & $somethingElse );
|
||||
|
||||
/* testBitwiseAndE */
|
||||
$a = [ 'a' => $first, 'b' => $something & $somethingElse ];
|
||||
|
||||
/* testBitwiseAndF */
|
||||
$a = array( 'a' => $first, 'b' => $something & \MyClass::$somethingElse );
|
||||
|
||||
/* testBitwiseAndG */
|
||||
$a = $something & $somethingElse;
|
||||
|
||||
/* testBitwiseAndH */
|
||||
function myFunction($a = 10 & 20) {}
|
||||
|
||||
/* testBitwiseAndI */
|
||||
$closure = function ($a = MY_CONSTANT & parent::OTHER_CONSTANT) {};
|
||||
|
||||
/* testFunctionReturnByReference */
|
||||
function &myFunction() {}
|
||||
|
||||
/* testFunctionPassByReferenceA */
|
||||
function myFunction( &$a ) {}
|
||||
|
||||
/* testFunctionPassByReferenceB */
|
||||
function myFunction( $a, &$b ) {}
|
||||
|
||||
/* testFunctionPassByReferenceC */
|
||||
$closure = function ( &$a ) {};
|
||||
|
||||
/* testFunctionPassByReferenceD */
|
||||
$closure = function ( $a, &$b ) {};
|
||||
|
||||
/* testFunctionPassByReferenceE */
|
||||
function myFunction(array &$one) {}
|
||||
|
||||
/* testFunctionPassByReferenceF */
|
||||
$closure = function (\MyClass &$one) {};
|
||||
|
||||
/* testFunctionPassByReferenceG */
|
||||
$closure = function ($param, &...$moreParams) {};
|
||||
|
||||
/* testForeachValueByReference */
|
||||
foreach( $array as $key => &$value ) {}
|
||||
|
||||
/* testForeachKeyByReference */
|
||||
foreach( $array as &$key => $value ) {}
|
||||
|
||||
/* testArrayValueByReferenceA */
|
||||
$a = [ 'a' => &$something ];
|
||||
|
||||
/* testArrayValueByReferenceB */
|
||||
$a = [ 'a' => $something, 'b' => &$somethingElse ];
|
||||
|
||||
/* testArrayValueByReferenceC */
|
||||
$a = [ &$something ];
|
||||
|
||||
/* testArrayValueByReferenceD */
|
||||
$a = [ $something, &$somethingElse ];
|
||||
|
||||
/* testArrayValueByReferenceE */
|
||||
$a = array( 'a' => &$something );
|
||||
|
||||
/* testArrayValueByReferenceF */
|
||||
$a = array( 'a' => $something, 'b' => &$somethingElse );
|
||||
|
||||
/* testArrayValueByReferenceG */
|
||||
$a = array( &$something );
|
||||
|
||||
/* testArrayValueByReferenceH */
|
||||
$a = array( $something, &$somethingElse );
|
||||
|
||||
/* testAssignByReferenceA */
|
||||
$b = &$something;
|
||||
|
||||
/* testAssignByReferenceB */
|
||||
$b =& $something;
|
||||
|
||||
/* testAssignByReferenceC */
|
||||
$b .= &$something;
|
||||
|
||||
/* testAssignByReferenceD */
|
||||
$myValue = &$obj->getValue();
|
||||
|
||||
/* testAssignByReferenceE */
|
||||
$collection = &collector();
|
||||
|
||||
/* testAssignByReferenceF */
|
||||
$collection ??= &collector();
|
||||
|
||||
/* testShortListAssignByReferenceNoKeyA */
|
||||
[
|
||||
&$a,
|
||||
/* testShortListAssignByReferenceNoKeyB */
|
||||
&$b,
|
||||
/* testNestedShortListAssignByReferenceNoKey */
|
||||
[$c, &$d]
|
||||
] = $array;
|
||||
|
||||
/* testLongListAssignByReferenceNoKeyA */
|
||||
list($a, &$b, list(/* testLongListAssignByReferenceNoKeyB */ &$c, /* testLongListAssignByReferenceNoKeyC */ &$d)) = $array;
|
||||
|
||||
[
|
||||
/* testNestedShortListAssignByReferenceWithKeyA */
|
||||
'a' => [&$a, $b],
|
||||
/* testNestedShortListAssignByReferenceWithKeyB */
|
||||
'b' => [$c, &$d]
|
||||
] = $array;
|
||||
|
||||
|
||||
/* testLongListAssignByReferenceWithKeyA */
|
||||
list(get_key()[1] => &$e) = [1, 2, 3];
|
||||
|
||||
/* testPassByReferenceA */
|
||||
functionCall(&$something, $somethingElse);
|
||||
|
||||
/* testPassByReferenceB */
|
||||
functionCall($something, &$somethingElse);
|
||||
|
||||
/* testPassByReferenceC */
|
||||
functionCall($something, &$this->somethingElse);
|
||||
|
||||
/* testPassByReferenceD */
|
||||
functionCall($something, &self::$somethingElse);
|
||||
|
||||
/* testPassByReferenceE */
|
||||
functionCall($something, &parent::$somethingElse);
|
||||
|
||||
/* testPassByReferenceF */
|
||||
functionCall($something, &static::$somethingElse);
|
||||
|
||||
/* testPassByReferenceG */
|
||||
functionCall($something, &SomeClass::$somethingElse);
|
||||
|
||||
/* testPassByReferenceH */
|
||||
functionCall(&\SomeClass::$somethingElse);
|
||||
|
||||
/* testPassByReferenceI */
|
||||
functionCall($something, &\SomeNS\SomeClass::$somethingElse);
|
||||
|
||||
/* testPassByReferenceJ */
|
||||
functionCall($something, &namespace\SomeClass::$somethingElse);
|
||||
|
||||
/* testPassByReferencePartiallyQualifiedName */
|
||||
functionCall($something, &Sub\Level\SomeClass::$somethingElse);
|
||||
|
||||
/* testNewByReferenceA */
|
||||
$foobar2 = &new Foobar();
|
||||
|
||||
/* testNewByReferenceB */
|
||||
functionCall( $something , &new Foobar() );
|
||||
|
||||
/* testUseByReference */
|
||||
$closure = function() use (&$var){};
|
||||
|
||||
/* testUseByReferenceWithCommentFirstParam */
|
||||
$closure = function() use /*comment*/ (&$this->value){};
|
||||
|
||||
/* testUseByReferenceWithCommentSecondParam */
|
||||
$closure = function() use /*comment*/ ($varA, &$varB){};
|
||||
|
||||
/* testArrowFunctionReturnByReference */
|
||||
fn&($x) => $x;
|
||||
|
||||
$closure = function (
|
||||
/* testBitwiseAndExactParameterA */
|
||||
$a = MY_CONSTANT & parent::OTHER_CONSTANT,
|
||||
/* testPassByReferenceExactParameterB */
|
||||
&$b,
|
||||
/* testPassByReferenceExactParameterC */
|
||||
&...$c,
|
||||
/* testBitwiseAndExactParameterD */
|
||||
$d = E_NOTICE & E_STRICT,
|
||||
) {};
|
||||
|
||||
// Issue PHPCS#3049.
|
||||
/* testArrowFunctionPassByReferenceA */
|
||||
$fn = fn(array &$one) => 1;
|
||||
|
||||
/* testArrowFunctionPassByReferenceB */
|
||||
$fn = fn($param, &...$moreParams) => 1;
|
||||
|
||||
/* testClosureReturnByReference */
|
||||
$closure = function &($param) use ($value) {};
|
||||
|
||||
/* testBitwiseAndArrowFunctionInDefault */
|
||||
$fn = fn( $one = E_NOTICE & E_STRICT) => 1;
|
||||
|
||||
/* testTokenizerIssue1284PHPCSlt280A */
|
||||
if ($foo) {}
|
||||
[&$a, /* testTokenizerIssue1284PHPCSlt280B */ &$b] = $c;
|
||||
|
||||
/* testTokenizerIssue1284PHPCSlt280C */
|
||||
if ($foo) {}
|
||||
[&$a, $b];
|
||||
-358
@@ -1,358 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::isReference method.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\File;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Files\File::isReference method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Files\File::isReference
|
||||
*/
|
||||
final class IsReferenceTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test that false is returned when a non-"bitwise and" token is passed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNotBitwiseAndToken()
|
||||
{
|
||||
$target = $this->getTargetToken('/* testBitwiseAndA */', T_STRING);
|
||||
$this->assertFalse(self::$phpcsFile->isReference($target));
|
||||
|
||||
}//end testNotBitwiseAndToken()
|
||||
|
||||
|
||||
/**
|
||||
* Test correctly identifying whether a "bitwise and" token is a reference or not.
|
||||
*
|
||||
* @param string $identifier Comment which precedes the test case.
|
||||
* @param bool $expected Expected function output.
|
||||
*
|
||||
* @dataProvider dataIsReference
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIsReference($identifier, $expected)
|
||||
{
|
||||
$bitwiseAnd = $this->getTargetToken($identifier, T_BITWISE_AND);
|
||||
$result = self::$phpcsFile->isReference($bitwiseAnd);
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testIsReference()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider for the IsReference test.
|
||||
*
|
||||
* @see testIsReference()
|
||||
*
|
||||
* @return array<string, array<string, string|bool>>
|
||||
*/
|
||||
public static function dataIsReference()
|
||||
{
|
||||
return [
|
||||
'issue-1971-list-first-in-file' => [
|
||||
'testMarker' => '/* testTokenizerIssue1971PHPCSlt330gt271A */',
|
||||
'expected' => true,
|
||||
],
|
||||
'issue-1971-list-first-in-file-nested' => [
|
||||
'testMarker' => '/* testTokenizerIssue1971PHPCSlt330gt271B */',
|
||||
'expected' => true,
|
||||
],
|
||||
'bitwise and: param in function call' => [
|
||||
'testMarker' => '/* testBitwiseAndA */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in unkeyed short array, first value' => [
|
||||
'testMarker' => '/* testBitwiseAndB */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in unkeyed short array, last value' => [
|
||||
'testMarker' => '/* testBitwiseAndC */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in unkeyed long array, last value' => [
|
||||
'testMarker' => '/* testBitwiseAndD */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in keyed short array, last value' => [
|
||||
'testMarker' => '/* testBitwiseAndE */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in keyed long array, last value' => [
|
||||
'testMarker' => '/* testBitwiseAndF */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in assignment' => [
|
||||
'testMarker' => '/* testBitwiseAndG */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in param default value in function declaration' => [
|
||||
'testMarker' => '/* testBitwiseAndH */',
|
||||
'expected' => false,
|
||||
],
|
||||
'bitwise and: in param default value in closure declaration' => [
|
||||
'testMarker' => '/* testBitwiseAndI */',
|
||||
'expected' => false,
|
||||
],
|
||||
'reference: function declared to return by reference' => [
|
||||
'testMarker' => '/* testFunctionReturnByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: only param in function declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: last param in function declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: only param in closure declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: last param in closure declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceD */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: typed param in function declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceE */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: typed param in closure declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceF */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variadic param in function declaration, pass by reference' => [
|
||||
'testMarker' => '/* testFunctionPassByReferenceG */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: foreach value' => [
|
||||
'testMarker' => '/* testForeachValueByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: foreach key' => [
|
||||
'testMarker' => '/* testForeachKeyByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed short array, first value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed short array, last value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed short array, only value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed short array, last value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceD */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed long array, first value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceE */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed long array, last value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceF */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed long array, only value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceG */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed long array, last value, value by reference' => [
|
||||
'testMarker' => '/* testArrayValueByReferenceH */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variable, assign by reference' => [
|
||||
'testMarker' => '/* testAssignByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variable, assign by reference, spacing variation' => [
|
||||
'testMarker' => '/* testAssignByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variable, assign by reference, concat assign' => [
|
||||
'testMarker' => '/* testAssignByReferenceC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: property, assign by reference' => [
|
||||
'testMarker' => '/* testAssignByReferenceD */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: function return value, assign by reference' => [
|
||||
'testMarker' => '/* testAssignByReferenceE */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: function return value, assign by reference, null coalesce assign' => [
|
||||
'testMarker' => '/* testAssignByReferenceF */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed short list, first var, assign by reference' => [
|
||||
'testMarker' => '/* testShortListAssignByReferenceNoKeyA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed short list, second var, assign by reference' => [
|
||||
'testMarker' => '/* testShortListAssignByReferenceNoKeyB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed short list, nested var, assign by reference' => [
|
||||
'testMarker' => '/* testNestedShortListAssignByReferenceNoKey */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed long list, second var, assign by reference' => [
|
||||
'testMarker' => '/* testLongListAssignByReferenceNoKeyA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed long list, first nested var, assign by reference' => [
|
||||
'testMarker' => '/* testLongListAssignByReferenceNoKeyB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: unkeyed long list, last nested var, assign by reference' => [
|
||||
'testMarker' => '/* testLongListAssignByReferenceNoKeyC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed short list, first nested var, assign by reference' => [
|
||||
'testMarker' => '/* testNestedShortListAssignByReferenceWithKeyA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed short list, last nested var, assign by reference' => [
|
||||
'testMarker' => '/* testNestedShortListAssignByReferenceWithKeyB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: keyed long list, only var, assign by reference' => [
|
||||
'testMarker' => '/* testLongListAssignByReferenceWithKeyA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: first param in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: last param in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: property in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: hierarchical self property in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceD */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: hierarchical parent property in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceE */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: hierarchical static property in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceF */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: static property in function call, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceG */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: static property in function call, first with FQN, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceH */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: static property in function call, last with FQN, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceI */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: static property in function call, last with namespace relative name, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceJ */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: static property in function call, last with PQN, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferencePartiallyQualifiedName */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: new by reference' => [
|
||||
'testMarker' => '/* testNewByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: new by reference as function call param' => [
|
||||
'testMarker' => '/* testNewByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: closure use by reference' => [
|
||||
'testMarker' => '/* testUseByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: closure use by reference, first param, with comment' => [
|
||||
'testMarker' => '/* testUseByReferenceWithCommentFirstParam */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: closure use by reference, last param, with comment' => [
|
||||
'testMarker' => '/* testUseByReferenceWithCommentSecondParam */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: arrow fn declared to return by reference' => [
|
||||
'testMarker' => '/* testArrowFunctionReturnByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'bitwise and: first param default value in closure declaration' => [
|
||||
'testMarker' => '/* testBitwiseAndExactParameterA */',
|
||||
'expected' => false,
|
||||
],
|
||||
'reference: param in closure declaration, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceExactParameterB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variadic param in closure declaration, pass by reference' => [
|
||||
'testMarker' => '/* testPassByReferenceExactParameterC */',
|
||||
'expected' => true,
|
||||
],
|
||||
'bitwise and: last param default value in closure declaration' => [
|
||||
'testMarker' => '/* testBitwiseAndExactParameterD */',
|
||||
'expected' => false,
|
||||
],
|
||||
'reference: typed param in arrow fn declaration, pass by reference' => [
|
||||
'testMarker' => '/* testArrowFunctionPassByReferenceA */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: variadic param in arrow fn declaration, pass by reference' => [
|
||||
'testMarker' => '/* testArrowFunctionPassByReferenceB */',
|
||||
'expected' => true,
|
||||
],
|
||||
'reference: closure declared to return by reference' => [
|
||||
'testMarker' => '/* testClosureReturnByReference */',
|
||||
'expected' => true,
|
||||
],
|
||||
'bitwise and: param default value in arrow fn declaration' => [
|
||||
'testMarker' => '/* testBitwiseAndArrowFunctionInDefault */',
|
||||
'expected' => false,
|
||||
],
|
||||
'issue-1284-short-list-directly-after-close-curly-control-structure' => [
|
||||
'testMarker' => '/* testTokenizerIssue1284PHPCSlt280A */',
|
||||
'expected' => true,
|
||||
],
|
||||
'issue-1284-short-list-directly-after-close-curly-control-structure-second-item' => [
|
||||
'testMarker' => '/* testTokenizerIssue1284PHPCSlt280B */',
|
||||
'expected' => true,
|
||||
],
|
||||
'issue-1284-short-array-directly-after-close-curly-control-structure' => [
|
||||
'testMarker' => '/* testTokenizerIssue1284PHPCSlt280C */',
|
||||
'expected' => true,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataIsReference()
|
||||
|
||||
|
||||
}//end class
|
||||
docker/streamline-src/vendor/squizlabs/php_codesniffer/tests/Core/Filters/AbstractFilterTestCase.php
Vendored
-227
@@ -1,227 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract Testcase class for testing Filters.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2023 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Filters;
|
||||
|
||||
use PHP_CodeSniffer\Filters\Filter;
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RecursiveIteratorIterator;
|
||||
|
||||
/**
|
||||
* Base functionality and utilities for testing Filter classes.
|
||||
*/
|
||||
abstract class AbstractFilterTestCase extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The Config object.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Config
|
||||
*/
|
||||
protected static $config;
|
||||
|
||||
/**
|
||||
* The Ruleset object.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
protected static $ruleset;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the config and ruleset objects.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initializeConfigAndRuleset()
|
||||
{
|
||||
self::$config = new ConfigDouble(['--extensions=php,inc/php,js,css']);
|
||||
self::$ruleset = new Ruleset(self::$config);
|
||||
|
||||
}//end initializeConfigAndRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Helper method to retrieve a mock object for a Filter class.
|
||||
*
|
||||
* The `setMethods()` method was silently deprecated in PHPUnit 9 and removed in PHPUnit 10.
|
||||
*
|
||||
* Note: direct access to the `getMockBuilder()` method is soft deprecated as of PHPUnit 10,
|
||||
* and expected to be hard deprecated in PHPUnit 11 and removed in PHPUnit 12.
|
||||
* Dealing with that is something for a later iteration of the test suite.
|
||||
*
|
||||
* @param string $className Fully qualified name of the class under test.
|
||||
* @param array<mixed> $constructorArgs Optional. Array of parameters to pass to the class constructor.
|
||||
* @param array<string>|null $methodsToMock Optional. The methods to mock in the class under test.
|
||||
* Needed for PHPUnit cross-version support as PHPUnit 4.x does
|
||||
* not have a `setMethodsExcept()` method yet.
|
||||
* If not passed, no methods will be replaced.
|
||||
*
|
||||
* @return \PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
protected function getMockedClass($className, array $constructorArgs=[], $methodsToMock=null)
|
||||
{
|
||||
$mockedObj = $this->getMockBuilder($className);
|
||||
|
||||
if (method_exists($mockedObj, 'onlyMethods') === true) {
|
||||
// PHPUnit 8+.
|
||||
if (is_array($methodsToMock) === true) {
|
||||
return $mockedObj
|
||||
->setConstructorArgs($constructorArgs)
|
||||
->onlyMethods($methodsToMock)
|
||||
->getMock();
|
||||
}
|
||||
|
||||
return $mockedObj->getMock()
|
||||
->setConstructorArgs($constructorArgs);
|
||||
}
|
||||
|
||||
// PHPUnit < 8.
|
||||
return $mockedObj
|
||||
->setConstructorArgs($constructorArgs)
|
||||
->setMethods($methodsToMock)
|
||||
->getMock();
|
||||
|
||||
}//end getMockedClass()
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve an array of files which were accepted by a filter.
|
||||
*
|
||||
* @param \PHP_CodeSniffer\Filters\Filter $filter The Filter object under test.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
protected function getFilteredResultsAsArray(Filter $filter)
|
||||
{
|
||||
$iterator = new RecursiveIteratorIterator($filter);
|
||||
$files = [];
|
||||
foreach ($iterator as $file) {
|
||||
$files[] = $file;
|
||||
}
|
||||
|
||||
return $files;
|
||||
|
||||
}//end getFilteredResultsAsArray()
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the basedir to use for tests using the `getFakeFileList()` method.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getBaseDir()
|
||||
{
|
||||
return dirname(dirname(dirname(__DIR__)));
|
||||
|
||||
}//end getBaseDir()
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve a file list containing a range of paths for testing purposes.
|
||||
*
|
||||
* This list **must** contain files which exist in this project (well, except for some which don't exist
|
||||
* purely for testing purposes), as `realpath()` is used in the logic under test and `realpath()` will
|
||||
* return `false` for any non-existent files, which will automatically filter them out before
|
||||
* we get to the code under test.
|
||||
*
|
||||
* Note this list does not include `.` and `..` as \PHP_CodeSniffer\Files\FileList uses `SKIP_DOTS`.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
protected static function getFakeFileList()
|
||||
{
|
||||
$basedir = self::getBaseDir();
|
||||
return [
|
||||
$basedir.'/.gitignore',
|
||||
$basedir.'/.yamllint.yml',
|
||||
$basedir.'/phpcs.xml',
|
||||
$basedir.'/phpcs.xml.dist',
|
||||
$basedir.'/autoload.php',
|
||||
$basedir.'/bin',
|
||||
$basedir.'/bin/phpcs',
|
||||
$basedir.'/bin/phpcs.bat',
|
||||
$basedir.'/scripts',
|
||||
$basedir.'/scripts/build-phar.php',
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/WillNotExist.php',
|
||||
$basedir.'/src/WillNotExist.bak',
|
||||
$basedir.'/src/WillNotExist.orig',
|
||||
$basedir.'/src/Ruleset.php',
|
||||
$basedir.'/src/Generators',
|
||||
$basedir.'/src/Generators/Markdown.php',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Generic',
|
||||
$basedir.'/src/Standards/Generic/Docs',
|
||||
$basedir.'/src/Standards/Generic/Docs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Docs/Classes/DuplicateClassNameStandard.xml',
|
||||
$basedir.'/src/Standards/Generic/Sniffs',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
$basedir.'/src/Standards/Generic/Tests',
|
||||
$basedir.'/src/Standards/Generic/Tests/Classes',
|
||||
$basedir.'/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.1.inc',
|
||||
// Will rarely exist when running the tests.
|
||||
$basedir.'/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.1.inc.bak',
|
||||
$basedir.'/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.2.inc',
|
||||
$basedir.'/src/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php',
|
||||
$basedir.'/src/Standards/Squiz',
|
||||
$basedir.'/src/Standards/Squiz/Docs',
|
||||
$basedir.'/src/Standards/Squiz/Docs/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Docs/WhiteSpace/SemicolonSpacingStandard.xml',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
|
||||
$basedir.'/src/Standards/Squiz/Tests',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
|
||||
];
|
||||
|
||||
}//end getFakeFileList()
|
||||
|
||||
|
||||
/**
|
||||
* Translate Linux paths to Windows paths, when necessary.
|
||||
*
|
||||
* These type of tests should be able to run and pass on both *nix as well as Windows
|
||||
* based dev systems. This method is a helper to allow for this.
|
||||
*
|
||||
* @param array<string|array> $paths A single or multi-dimensional array containing
|
||||
* file paths.
|
||||
*
|
||||
* @return array<string|array>
|
||||
*/
|
||||
protected static function mapPathsToRuntimeOs(array $paths)
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
return $paths;
|
||||
}
|
||||
|
||||
foreach ($paths as $key => $value) {
|
||||
if (is_string($value) === true) {
|
||||
$paths[$key] = strtr($value, '/', '\\\\');
|
||||
} else if (is_array($value) === true) {
|
||||
$paths[$key] = self::mapPathsToRuntimeOs($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
|
||||
}//end mapPathsToRuntimeOs()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-110
@@ -1,110 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
|
||||
*
|
||||
* @author Willington Vega <wvega@wvega.com>
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Filters\Filter;
|
||||
|
||||
use PHP_CodeSniffer\Filters\Filter;
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
|
||||
use RecursiveArrayIterator;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\Filter::accept method.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Filters\Filter
|
||||
*/
|
||||
final class AcceptTest extends AbstractFilterTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the config and ruleset objects based on the `AcceptTest.xml` ruleset file.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initializeConfigAndRuleset()
|
||||
{
|
||||
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
|
||||
self::$config = new ConfigDouble(["--standard=$standard", '--ignore=*/somethingelse/*']);
|
||||
self::$ruleset = new Ruleset(self::$config);
|
||||
|
||||
}//end initializeConfigAndRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @param array<string> $inputPaths List of file paths to be filtered.
|
||||
* @param array<string> $expectedOutput Expected filtering result.
|
||||
*
|
||||
* @dataProvider dataExcludePatterns
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExcludePatterns($inputPaths, $expectedOutput)
|
||||
{
|
||||
$fakeDI = new RecursiveArrayIterator($inputPaths);
|
||||
$filter = new Filter($fakeDI, '/', self::$config, self::$ruleset);
|
||||
|
||||
$this->assertEquals($expectedOutput, $this->getFilteredResultsAsArray($filter));
|
||||
|
||||
}//end testExcludePatterns()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testExcludePatterns
|
||||
*
|
||||
* @return array<string, array<string, array<string>>>
|
||||
*/
|
||||
public static function dataExcludePatterns()
|
||||
{
|
||||
$testCases = [
|
||||
// Test top-level exclude patterns.
|
||||
'Non-sniff specific path based excludes from ruleset and command line are respected and don\'t filter out too much' => [
|
||||
'inputPaths' => [
|
||||
'/path/to/src/Main.php',
|
||||
'/path/to/src/Something/Main.php',
|
||||
'/path/to/src/Somethingelse/Main.php',
|
||||
'/path/to/src/SomethingelseEvenLonger/Main.php',
|
||||
'/path/to/src/Other/Main.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
'/path/to/src/Main.php',
|
||||
'/path/to/src/SomethingelseEvenLonger/Main.php',
|
||||
],
|
||||
],
|
||||
|
||||
// Test ignoring standard/sniff specific exclude patterns.
|
||||
'Filter should not act on standard/sniff specific exclude patterns' => [
|
||||
'inputPaths' => [
|
||||
'/path/to/src/generic-project/Main.php',
|
||||
'/path/to/src/generic/Main.php',
|
||||
'/path/to/src/anything-generic/Main.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
'/path/to/src/generic-project/Main.php',
|
||||
'/path/to/src/generic/Main.php',
|
||||
'/path/to/src/anything-generic/Main.php',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// Allow these tests to work on Windows as well.
|
||||
return self::mapPathsToRuntimeOs($testCases);
|
||||
|
||||
}//end dataExcludePatterns()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-16
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="AcceptTest" xsi:noNamespaceSchemaLocation="phpcs.xsd">
|
||||
<description>Ruleset to test the filtering based on exclude patterns.</description>
|
||||
|
||||
<!-- Directory pattern. -->
|
||||
<exclude-pattern>*/something/*</exclude-pattern>
|
||||
<!-- File pattern. -->
|
||||
<exclude-pattern>*/Other/Main\.php$</exclude-pattern>
|
||||
|
||||
<rule ref="Generic">
|
||||
<!-- Standard specific directory pattern. -->
|
||||
<exclude-pattern>/anything/*</exclude-pattern>
|
||||
<!-- Standard specific file pattern. -->
|
||||
<exclude-pattern>/YetAnother/Main\.php</exclude-pattern>
|
||||
</rule>
|
||||
</ruleset>
|
||||
Vendored
-268
@@ -1,268 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\GitModified class.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2023 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Filters;
|
||||
|
||||
use PHP_CodeSniffer\Filters\GitModified;
|
||||
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
|
||||
use RecursiveArrayIterator;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\GitModified class.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Filters\GitModified
|
||||
*/
|
||||
final class GitModifiedTest extends AbstractFilterTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFileNamePassesAsBasePathWillTranslateToDirname()
|
||||
{
|
||||
$rootFile = self::getBaseDir().'/autoload.php';
|
||||
|
||||
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
|
||||
$constructorArgs = [
|
||||
$fakeDI,
|
||||
$rootFile,
|
||||
self::$config,
|
||||
self::$ruleset,
|
||||
];
|
||||
$mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitModified', $constructorArgs, ['exec']);
|
||||
|
||||
$mockObj->expects($this->once())
|
||||
->method('exec')
|
||||
->willReturn(['autoload.php']);
|
||||
|
||||
$this->assertEquals([$rootFile], $this->getFilteredResultsAsArray($mockObj));
|
||||
|
||||
}//end testFileNamePassesAsBasePathWillTranslateToDirname()
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @param array<string> $inputPaths List of file paths to be filtered.
|
||||
* @param array<string> $outputGitModified Simulated "git modified" output.
|
||||
* @param array<string> $expectedOutput Expected filtering result.
|
||||
*
|
||||
* @dataProvider dataAcceptOnlyGitModified
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAcceptOnlyGitModified($inputPaths, $outputGitModified, $expectedOutput)
|
||||
{
|
||||
$fakeDI = new RecursiveArrayIterator($inputPaths);
|
||||
$constructorArgs = [
|
||||
$fakeDI,
|
||||
self::getBaseDir(),
|
||||
self::$config,
|
||||
self::$ruleset,
|
||||
];
|
||||
$mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitModified', $constructorArgs, ['exec']);
|
||||
|
||||
$mockObj->expects($this->once())
|
||||
->method('exec')
|
||||
->willReturn($outputGitModified);
|
||||
|
||||
$this->assertEquals($expectedOutput, $this->getFilteredResultsAsArray($mockObj));
|
||||
|
||||
}//end testAcceptOnlyGitModified()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAcceptOnlyGitModified
|
||||
*
|
||||
* @return array<string, array<string, array<string>>>
|
||||
*/
|
||||
public static function dataAcceptOnlyGitModified()
|
||||
{
|
||||
$basedir = self::getBaseDir();
|
||||
$fakeFileList = self::getFakeFileList();
|
||||
|
||||
$testCases = [
|
||||
'no files marked as git modified' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [],
|
||||
'expectedOutput' => [],
|
||||
],
|
||||
|
||||
'files marked as git modified which don\'t actually exist' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'src/WillNotExist.php',
|
||||
'src/WillNotExist.bak',
|
||||
'src/WillNotExist.orig',
|
||||
],
|
||||
'expectedOutput' => [],
|
||||
],
|
||||
|
||||
'single file marked as git modified - file in root dir' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'autoload.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/autoload.php',
|
||||
],
|
||||
],
|
||||
'single file marked as git modified - file in sub dir' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Generic',
|
||||
$basedir.'/src/Standards/Generic/Sniffs',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, none valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'.gitignore',
|
||||
'phpcs.xml.dist',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Squiz',
|
||||
$basedir.'/src/Standards/Squiz/Tests',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, only one file valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'.gitignore',
|
||||
'src/Standards/Generic/Docs/Classes/DuplicateClassNameStandard.xml',
|
||||
'src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Generic',
|
||||
$basedir.'/src/Standards/Generic/Docs',
|
||||
$basedir.'/src/Standards/Generic/Docs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, multiple files valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitModified' => [
|
||||
'.yamllint.yml',
|
||||
'autoload.php',
|
||||
'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/autoload.php',
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Squiz',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
|
||||
$basedir.'/src/Standards/Squiz/Tests',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $testCases;
|
||||
|
||||
}//end dataAcceptOnlyGitModified()
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @param string $cmd Command to run.
|
||||
* @param array<string> $expected Expected return value.
|
||||
*
|
||||
* @dataProvider dataExecAlwaysReturnsArray
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExecAlwaysReturnsArray($cmd, $expected)
|
||||
{
|
||||
if (is_dir(__DIR__.'/../../../.git') === false) {
|
||||
$this->markTestSkipped('Not a git repository');
|
||||
}
|
||||
|
||||
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
|
||||
$filter = new GitModified($fakeDI, '/', self::$config, self::$ruleset);
|
||||
|
||||
$reflMethod = new ReflectionMethod($filter, 'exec');
|
||||
$reflMethod->setAccessible(true);
|
||||
$result = $reflMethod->invoke($filter, $cmd);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testExecAlwaysReturnsArray()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testExecAlwaysReturnsArray
|
||||
*
|
||||
* {@internal Missing: test with a command which yields a `false` return value.
|
||||
* JRF: I've not managed to find a command which does so, let alone one, which then
|
||||
* doesn't have side-effects of uncatchable output while running the tests.}
|
||||
*
|
||||
* @return array<string, array<string, string|array<string>>>
|
||||
*/
|
||||
public static function dataExecAlwaysReturnsArray()
|
||||
{
|
||||
return [
|
||||
'valid command which won\'t have any output unless files in the bin dir have been modified' => [
|
||||
// Largely using the command used in the filter, but only checking the bin dir.
|
||||
// This should prevent the test unexpectedly failing during local development (in most cases).
|
||||
'cmd' => 'git ls-files -o -m --exclude-standard -- '.escapeshellarg(self::getBaseDir().'/bin'),
|
||||
'expected' => [],
|
||||
],
|
||||
'valid command which will have output' => [
|
||||
'cmd' => 'git ls-files --exclude-standard -- '.escapeshellarg(self::getBaseDir().'/bin'),
|
||||
'expected' => [
|
||||
'bin/phpcbf',
|
||||
'bin/phpcbf.bat',
|
||||
'bin/phpcs',
|
||||
'bin/phpcs.bat',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataExecAlwaysReturnsArray()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-268
@@ -1,268 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\GitStaged class.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2023 PHPCSStandards Contributors
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Filters;
|
||||
|
||||
use PHP_CodeSniffer\Filters\GitStaged;
|
||||
use PHP_CodeSniffer\Tests\Core\Filters\AbstractFilterTestCase;
|
||||
use RecursiveArrayIterator;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Filters\GitStaged class.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Filters\GitStaged
|
||||
*/
|
||||
final class GitStagedTest extends AbstractFilterTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testFileNamePassesAsBasePathWillTranslateToDirname()
|
||||
{
|
||||
$rootFile = self::getBaseDir().'/autoload.php';
|
||||
|
||||
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
|
||||
$constructorArgs = [
|
||||
$fakeDI,
|
||||
$rootFile,
|
||||
self::$config,
|
||||
self::$ruleset,
|
||||
];
|
||||
$mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitStaged', $constructorArgs, ['exec']);
|
||||
|
||||
$mockObj->expects($this->once())
|
||||
->method('exec')
|
||||
->willReturn(['autoload.php']);
|
||||
|
||||
$this->assertEquals([$rootFile], $this->getFilteredResultsAsArray($mockObj));
|
||||
|
||||
}//end testFileNamePassesAsBasePathWillTranslateToDirname()
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @param array<string> $inputPaths List of file paths to be filtered.
|
||||
* @param array<string> $outputGitStaged Simulated "git staged" output.
|
||||
* @param array<string> $expectedOutput Expected filtering result.
|
||||
*
|
||||
* @dataProvider dataAcceptOnlyGitStaged
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAcceptOnlyGitStaged($inputPaths, $outputGitStaged, $expectedOutput)
|
||||
{
|
||||
$fakeDI = new RecursiveArrayIterator($inputPaths);
|
||||
$constructorArgs = [
|
||||
$fakeDI,
|
||||
self::getBaseDir(),
|
||||
self::$config,
|
||||
self::$ruleset,
|
||||
];
|
||||
$mockObj = $this->getMockedClass('PHP_CodeSniffer\Filters\GitStaged', $constructorArgs, ['exec']);
|
||||
|
||||
$mockObj->expects($this->once())
|
||||
->method('exec')
|
||||
->willReturn($outputGitStaged);
|
||||
|
||||
$this->assertEquals($expectedOutput, $this->getFilteredResultsAsArray($mockObj));
|
||||
|
||||
}//end testAcceptOnlyGitStaged()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAcceptOnlyGitStaged
|
||||
*
|
||||
* @return array<string, array<string, array<string>>>
|
||||
*/
|
||||
public static function dataAcceptOnlyGitStaged()
|
||||
{
|
||||
$basedir = self::getBaseDir();
|
||||
$fakeFileList = self::getFakeFileList();
|
||||
|
||||
$testCases = [
|
||||
'no files marked as git modified' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [],
|
||||
'expectedOutput' => [],
|
||||
],
|
||||
|
||||
'files marked as git modified which don\'t actually exist' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'src/WillNotExist.php',
|
||||
'src/WillNotExist.bak',
|
||||
'src/WillNotExist.orig',
|
||||
],
|
||||
'expectedOutput' => [],
|
||||
],
|
||||
|
||||
'single file marked as git modified - file in root dir' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'autoload.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/autoload.php',
|
||||
],
|
||||
],
|
||||
'single file marked as git modified - file in sub dir' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Generic',
|
||||
$basedir.'/src/Standards/Generic/Sniffs',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, none valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'.gitignore',
|
||||
'phpcs.xml.dist',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Squiz',
|
||||
$basedir.'/src/Standards/Squiz/Tests',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, only one file valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'.gitignore',
|
||||
'src/Standards/Generic/Docs/Classes/DuplicateClassNameStandard.xml',
|
||||
'src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Generic',
|
||||
$basedir.'/src/Standards/Generic/Docs',
|
||||
$basedir.'/src/Standards/Generic/Docs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes',
|
||||
$basedir.'/src/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php',
|
||||
],
|
||||
],
|
||||
|
||||
'multiple files marked as git modified, multiple files valid for scan' => [
|
||||
'inputPaths' => $fakeFileList,
|
||||
'outputGitStaged' => [
|
||||
'.yamllint.yml',
|
||||
'autoload.php',
|
||||
'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
|
||||
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
|
||||
],
|
||||
'expectedOutput' => [
|
||||
$basedir.'/autoload.php',
|
||||
$basedir.'/src',
|
||||
$basedir.'/src/Standards',
|
||||
$basedir.'/src/Standards/Squiz',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
|
||||
$basedir.'/src/Standards/Squiz/Tests',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
|
||||
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $testCases;
|
||||
|
||||
}//end dataAcceptOnlyGitStaged()
|
||||
|
||||
|
||||
/**
|
||||
* Test filtering a file list for excluded paths.
|
||||
*
|
||||
* @param string $cmd Command to run.
|
||||
* @param array<string> $expected Expected return value.
|
||||
*
|
||||
* @dataProvider dataExecAlwaysReturnsArray
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExecAlwaysReturnsArray($cmd, $expected)
|
||||
{
|
||||
if (is_dir(__DIR__.'/../../../.git') === false) {
|
||||
$this->markTestSkipped('Not a git repository');
|
||||
}
|
||||
|
||||
$fakeDI = new RecursiveArrayIterator(self::getFakeFileList());
|
||||
$filter = new GitStaged($fakeDI, '/', self::$config, self::$ruleset);
|
||||
|
||||
$reflMethod = new ReflectionMethod($filter, 'exec');
|
||||
$reflMethod->setAccessible(true);
|
||||
$result = $reflMethod->invoke($filter, $cmd);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
|
||||
}//end testExecAlwaysReturnsArray()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testExecAlwaysReturnsArray
|
||||
*
|
||||
* {@internal Missing: test with a command which yields a `false` return value.
|
||||
* JRF: I've not managed to find a command which does so, let alone one, which then
|
||||
* doesn't have side-effects of uncatchable output while running the tests.}
|
||||
*
|
||||
* @return array<string, array<string, array<string>>>
|
||||
*/
|
||||
public static function dataExecAlwaysReturnsArray()
|
||||
{
|
||||
return [
|
||||
'valid command which won\'t have any output unless files in the bin dir have been modified & staged' => [
|
||||
// Largely using the command used in the filter, but only checking the bin dir.
|
||||
// This should prevent the test unexpectedly failing during local development (in most cases).
|
||||
'cmd' => 'git diff --cached --name-only -- '.escapeshellarg(self::getBaseDir().'/bin'),
|
||||
'expected' => [],
|
||||
],
|
||||
'valid command which will have output' => [
|
||||
'cmd' => 'git ls-files --exclude-standard -- '.escapeshellarg(self::getBaseDir().'/bin'),
|
||||
'expected' => [
|
||||
'bin/phpcbf',
|
||||
'bin/phpcbf.bat',
|
||||
'bin/phpcs',
|
||||
'bin/phpcs.bat',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataExecAlwaysReturnsArray()
|
||||
|
||||
|
||||
}//end class
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExplainCustomRulesetTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="PSR12.ControlStructures"/>
|
||||
<rule ref="Squiz.Scope.MethodScope"/>
|
||||
<rule ref="PSR1">
|
||||
<exclude name="PSR1.Files.SideEffects"/>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
docker/streamline-src/vendor/squizlabs/php_codesniffer/tests/Core/Ruleset/ExplainSingleSniffTest.xml
Vendored
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ExplainSingleSniffTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="Squiz.Scope.MethodScope"/>
|
||||
|
||||
</ruleset>
|
||||
-258
@@ -1,258 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests to verify that the "explain" command functions as expected.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2023 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Runner;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test the Ruleset::explain() function.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset::explain
|
||||
*/
|
||||
final class ExplainTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test the output of the "explain" command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplain()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$config = new ConfigDouble(['--standard=PSR1', '-e']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
|
||||
$expected .= '------------------'.PHP_EOL;
|
||||
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
|
||||
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
|
||||
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
|
||||
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->explain();
|
||||
|
||||
}//end testExplain()
|
||||
|
||||
|
||||
/**
|
||||
* Test the output of the "explain" command is not influenced by a user set report width.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplainAlwaysDisplaysCompleteSniffName()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$config = new ConfigDouble(['--standard=PSR1', '-e', '--report-width=30']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
|
||||
$expected .= '------------------'.PHP_EOL;
|
||||
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
|
||||
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
|
||||
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
|
||||
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->explain();
|
||||
|
||||
}//end testExplainAlwaysDisplaysCompleteSniffName()
|
||||
|
||||
|
||||
/**
|
||||
* Test the output of the "explain" command when a ruleset only contains a single sniff.
|
||||
*
|
||||
* This is mostly about making sure that the summary line uses the correct grammar.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplainSingleSniff()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard", '-e']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->explain();
|
||||
|
||||
}//end testExplainSingleSniff()
|
||||
|
||||
|
||||
/**
|
||||
* Test that "explain" works correctly with custom rulesets.
|
||||
*
|
||||
* Verifies that:
|
||||
* - The "standard" name is taken from the custom ruleset.
|
||||
* - Any and all sniff additions and exclusions in the ruleset are taken into account correctly.
|
||||
* - That the displayed list will have both the standards as well as the sniff names
|
||||
* ordered alphabetically.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplainCustomRuleset()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/ExplainCustomRulesetTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard", '-e']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The ExplainCustomRulesetTest standard contains 10 sniffs'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
|
||||
$expected .= '------------------'.PHP_EOL;
|
||||
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
|
||||
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'PSR1 (2 sniffs)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
|
||||
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'PSR12 (2 sniffs)'.PHP_EOL;
|
||||
$expected .= '----------------'.PHP_EOL;
|
||||
$expected .= ' PSR12.ControlStructures.BooleanOperatorPlacement'.PHP_EOL;
|
||||
$expected .= ' PSR12.ControlStructures.ControlStructureSpacing'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (2 sniffs)'.PHP_EOL;
|
||||
$expected .= '----------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL;
|
||||
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->explain();
|
||||
|
||||
}//end testExplainCustomRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Test the output of the "explain" command for a standard containing both deprecated
|
||||
* and non-deprecated sniffs.
|
||||
*
|
||||
* Tests that:
|
||||
* - Deprecated sniffs are marked with an asterix in the list.
|
||||
* - A footnote is displayed explaining the asterix.
|
||||
* - And that the "standard uses # deprecated sniffs" listing is **not** displayed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplainWithDeprecatedSniffs()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__."/ShowSniffDeprecationsTest.xml";
|
||||
$config = new ConfigDouble(["--standard=$standard", '-e']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The SniffDeprecationTest standard contains 9 sniffs'.PHP_EOL.PHP_EOL;
|
||||
|
||||
$expected .= 'Fixtures (9 sniffs)'.PHP_EOL;
|
||||
$expected .= '-------------------'.PHP_EOL;
|
||||
$expected .= ' Fixtures.Deprecated.WithLongReplacement *'.PHP_EOL;
|
||||
$expected .= ' Fixtures.Deprecated.WithoutReplacement *'.PHP_EOL;
|
||||
$expected .= ' Fixtures.Deprecated.WithReplacement *'.PHP_EOL;
|
||||
$expected .= ' Fixtures.Deprecated.WithReplacementContainingLinuxNewlines *'.PHP_EOL;
|
||||
$expected .= ' Fixtures.Deprecated.WithReplacementContainingNewlines *'.PHP_EOL;
|
||||
$expected .= ' Fixtures.SetProperty.AllowedAsDeclared'.PHP_EOL;
|
||||
$expected .= ' Fixtures.SetProperty.AllowedViaMagicMethod'.PHP_EOL;
|
||||
$expected .= ' Fixtures.SetProperty.AllowedViaStdClass'.PHP_EOL;
|
||||
$expected .= ' Fixtures.SetProperty.NotAllowedViaAttribute'.PHP_EOL.PHP_EOL;
|
||||
|
||||
$expected .= '* Sniffs marked with an asterix are deprecated.'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->explain();
|
||||
|
||||
}//end testExplainWithDeprecatedSniffs()
|
||||
|
||||
|
||||
/**
|
||||
* Test that each standard passed on the command-line is explained separately.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Runner::runPHPCS
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExplainWillExplainEachStandardSeparately()
|
||||
{
|
||||
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
|
||||
$_SERVER['argv'] = [
|
||||
'phpcs',
|
||||
'-e',
|
||||
"--standard=PSR1,$standard",
|
||||
'--report-width=80',
|
||||
];
|
||||
|
||||
$expected = PHP_EOL;
|
||||
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
|
||||
$expected .= '------------------'.PHP_EOL;
|
||||
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
|
||||
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
|
||||
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
|
||||
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
|
||||
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL.PHP_EOL;
|
||||
|
||||
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
|
||||
$expected .= '---------------'.PHP_EOL;
|
||||
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$runner = new Runner();
|
||||
$exitCode = $runner->runPHPCS();
|
||||
|
||||
}//end testExplainWillExplainEachStandardSeparately()
|
||||
|
||||
|
||||
}//end class
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\Deprecated;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class WithLongReplacementSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'v3.8.0';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'v4.0.0';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed. Fusce egestas congue massa semper cursus. Donec quis pretium tellus. In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan eros sapien at sem. Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum lectus at egestas. Fusce tempus elit in tincidunt dictum. Suspendisse dictum egestas sapien, eget ullamcorper metus elementum semper. Vestibulum sem justo, consectetur ac tincidunt et, finibus eget libero.';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\Deprecated;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class WithReplacementContainingLinuxNewlinesSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'v3.8.0';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'v4.0.0';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"
|
||||
."Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed.\n"
|
||||
."Fusce egestas congue massa semper cursus. Donec quis pretium tellus.\n"
|
||||
."In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan eros sapien at sem.\n"
|
||||
.'Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum lectus at egestas.';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\Deprecated;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class WithReplacementContainingNewlinesSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'v3.8.0';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'v4.0.0';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'.PHP_EOL
|
||||
.'Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed.'.PHP_EOL
|
||||
.'Fusce egestas congue massa semper cursus. Donec quis pretium tellus.'.PHP_EOL
|
||||
.'In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan eros sapien at sem.'.PHP_EOL
|
||||
.'Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum lectus at egestas';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\Deprecated;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class WithReplacementSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'v3.8.0';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'v4.0.0';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'Use the Stnd.Category.OtherSniff sniff instead.';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\Deprecated;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class WithoutReplacementSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'v3.4.0';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'v4.0.0';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\DeprecatedInvalid;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class EmptyDeprecationVersionSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\DeprecatedInvalid;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class EmptyRemovalVersionSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\DeprecatedInvalid;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use stdClass;
|
||||
|
||||
class InvalidDeprecationMessageSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return new stdClass;
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\DeprecatedInvalid;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class InvalidDeprecationVersionSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 3.8;
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SniffDeprecationTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\DeprecatedInvalid;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class InvalidRemovalVersionSniff implements Sniff,DeprecatedSniff
|
||||
{
|
||||
|
||||
public function getDeprecationVersion()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function getRemovalVersion()
|
||||
{
|
||||
return ['4.0'];
|
||||
}
|
||||
|
||||
public function getDeprecationMessage()
|
||||
{
|
||||
return 'dummy';
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\SetProperty;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class AllowedAsDeclaredSniff implements Sniff
|
||||
{
|
||||
|
||||
public $arbitrarystring;
|
||||
public $arbitraryarray;
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\SetProperty;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
class AllowedViaMagicMethodSniff implements Sniff
|
||||
{
|
||||
private $magic = [];
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->magic[$name] = $value;
|
||||
}
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->magic[$name])) {
|
||||
return $this->magic[$name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\SetProperty;
|
||||
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
use stdClass;
|
||||
|
||||
class AllowedViaStdClassSniff extends stdClass implements Sniff
|
||||
{
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Test fixture.
|
||||
*
|
||||
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\SetSniffPropertyTest
|
||||
*/
|
||||
|
||||
namespace Fixtures\Sniffs\SetProperty;
|
||||
|
||||
use AllowDynamicProperties;
|
||||
use PHP_CodeSniffer\Files\File;
|
||||
use PHP_CodeSniffer\Sniffs\Sniff;
|
||||
|
||||
#[AllowDynamicProperties]
|
||||
class NotAllowedViaAttributeSniff implements Sniff
|
||||
{
|
||||
|
||||
public function register()
|
||||
{
|
||||
return [T_WHITESPACE];
|
||||
}
|
||||
|
||||
public function process(File $phpcsFile, $stackPtr)
|
||||
{
|
||||
// Do something.
|
||||
}
|
||||
}
|
||||
Vendored
-4
@@ -1,4 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
</ruleset>
|
||||
-119
@@ -1,119 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class using a Linux-style absolute path to include a sniff.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class using a Linux-style absolute path to include a sniff.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
final class RuleInclusionAbsoluteLinuxTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The Ruleset object.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
protected $ruleset;
|
||||
|
||||
/**
|
||||
* Path to the ruleset file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $standard = '';
|
||||
|
||||
/**
|
||||
* The original content of the ruleset.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $contents = '';
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the config and ruleset objects.
|
||||
*
|
||||
* @before
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initializeConfigAndRuleset()
|
||||
{
|
||||
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
|
||||
$repoRootDir = dirname(dirname(dirname(__DIR__)));
|
||||
|
||||
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
|
||||
$contents = file_get_contents($this->standard);
|
||||
$this->contents = $contents;
|
||||
|
||||
$newPath = $repoRootDir;
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
$newPath = str_replace('\\', '/', $repoRootDir);
|
||||
}
|
||||
|
||||
$adjusted = str_replace('%path_slash_forward%', $newPath, $contents);
|
||||
|
||||
if (file_put_contents($this->standard, $adjusted) === false) {
|
||||
$this->markTestSkipped('On the fly ruleset adjustment failed');
|
||||
}
|
||||
|
||||
// Initialize the config and ruleset objects for the test.
|
||||
$config = new ConfigDouble(["--standard={$this->standard}"]);
|
||||
$this->ruleset = new Ruleset($config);
|
||||
|
||||
}//end initializeConfigAndRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Reset ruleset file.
|
||||
*
|
||||
* @after
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetRuleset()
|
||||
{
|
||||
file_put_contents($this->standard, $this->contents);
|
||||
|
||||
}//end resetRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Test that sniffs registed with a Linux absolute path are correctly recognized and that
|
||||
* properties are correctly set for them.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testLinuxStylePathRuleInclusion()
|
||||
{
|
||||
// Test that the sniff is correctly registered.
|
||||
$this->assertCount(1, $this->ruleset->sniffCodes);
|
||||
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterNot', $this->ruleset->sniffCodes);
|
||||
$this->assertSame(
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff',
|
||||
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterNot']
|
||||
);
|
||||
|
||||
// Test that the sniff properties are correctly set.
|
||||
$this->assertSame(
|
||||
'10',
|
||||
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff']->spacing
|
||||
);
|
||||
|
||||
}//end testLinuxStylePathRuleInclusion()
|
||||
|
||||
|
||||
}//end class
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteLinuxTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<!-- %path_slash_forward% will be replaced on the fly -->
|
||||
<rule ref="%path_slash_forward%/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php">
|
||||
<properties>
|
||||
<property name="spacing" value="10" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-120
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class using a Windows-style absolute path to include a sniff.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class using a Windows-style absolute path to include a sniff.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
final class RuleInclusionAbsoluteWindowsTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The Ruleset object.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
protected $ruleset;
|
||||
|
||||
/**
|
||||
* Path to the ruleset file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $standard = '';
|
||||
|
||||
/**
|
||||
* The original content of the ruleset.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $contents = '';
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the config and ruleset objects.
|
||||
*
|
||||
* @before
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initializeConfigAndRuleset()
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR === '/') {
|
||||
$this->markTestSkipped('Windows specific test');
|
||||
}
|
||||
|
||||
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
|
||||
$repoRootDir = dirname(dirname(dirname(__DIR__)));
|
||||
|
||||
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
|
||||
$contents = file_get_contents($this->standard);
|
||||
$this->contents = $contents;
|
||||
|
||||
$adjusted = str_replace('%path_slash_back%', $repoRootDir, $contents);
|
||||
|
||||
if (file_put_contents($this->standard, $adjusted) === false) {
|
||||
$this->markTestSkipped('On the fly ruleset adjustment failed');
|
||||
}
|
||||
|
||||
// Initialize the config and ruleset objects for the test.
|
||||
$config = new ConfigDouble(["--standard={$this->standard}"]);
|
||||
$this->ruleset = new Ruleset($config);
|
||||
|
||||
}//end initializeConfigAndRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Reset ruleset file.
|
||||
*
|
||||
* @after
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetRuleset()
|
||||
{
|
||||
if (DIRECTORY_SEPARATOR !== '/') {
|
||||
file_put_contents($this->standard, $this->contents);
|
||||
}
|
||||
|
||||
}//end resetRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Test that sniffs registed with a Windows absolute path are correctly recognized and that
|
||||
* properties are correctly set for them.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testWindowsStylePathRuleInclusion()
|
||||
{
|
||||
// Test that the sniff is correctly registered.
|
||||
$this->assertCount(1, $this->ruleset->sniffCodes);
|
||||
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterCast', $this->ruleset->sniffCodes);
|
||||
$this->assertSame(
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff',
|
||||
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterCast']
|
||||
);
|
||||
|
||||
// Test that the sniff property is correctly set.
|
||||
$this->assertSame(
|
||||
'10',
|
||||
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff']->spacing
|
||||
);
|
||||
|
||||
}//end testWindowsStylePathRuleInclusion()
|
||||
|
||||
|
||||
}//end class
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteWindowsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<!-- %path_slash_back% will be replaced on the fly -->
|
||||
<rule ref="%path_slash_back%\src\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff.php">
|
||||
<properties>
|
||||
<property name="spacing" value="10" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionTest-include" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="Generic.Metrics.NestingLevel">
|
||||
<properties>
|
||||
<property name="nestingLevel" value="2" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
Vendored
-478
@@ -1,478 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionObject;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Ruleset class.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
final class RuleInclusionTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The Ruleset object.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Ruleset
|
||||
*/
|
||||
protected static $ruleset;
|
||||
|
||||
/**
|
||||
* Path to the ruleset file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $standard = '';
|
||||
|
||||
/**
|
||||
* The original content of the ruleset.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $contents = '';
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the config and ruleset objects based on the `RuleInclusionTest.xml` ruleset file.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initializeConfigAndRuleset()
|
||||
{
|
||||
$standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
|
||||
self::$standard = $standard;
|
||||
|
||||
// On-the-fly adjust the ruleset test file to be able to test
|
||||
// sniffs included with relative paths.
|
||||
$contents = file_get_contents($standard);
|
||||
self::$contents = $contents;
|
||||
|
||||
$repoRootDir = basename(dirname(dirname(dirname(__DIR__))));
|
||||
|
||||
$newPath = $repoRootDir;
|
||||
if (DIRECTORY_SEPARATOR === '\\') {
|
||||
$newPath = str_replace('\\', '/', $repoRootDir);
|
||||
}
|
||||
|
||||
$adjusted = str_replace('%path_root_dir%', $newPath, $contents);
|
||||
|
||||
if (file_put_contents($standard, $adjusted) === false) {
|
||||
self::markTestSkipped('On the fly ruleset adjustment failed');
|
||||
}
|
||||
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
self::$ruleset = new Ruleset($config);
|
||||
|
||||
}//end initializeConfigAndRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Reset ruleset file.
|
||||
*
|
||||
* @after
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function resetRuleset()
|
||||
{
|
||||
file_put_contents(self::$standard, self::$contents);
|
||||
|
||||
}//end resetRuleset()
|
||||
|
||||
|
||||
/**
|
||||
* Test that sniffs are registered.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHasSniffCodes()
|
||||
{
|
||||
$this->assertCount(48, self::$ruleset->sniffCodes);
|
||||
|
||||
}//end testHasSniffCodes()
|
||||
|
||||
|
||||
/**
|
||||
* Test that sniffs are correctly registered, independently of the syntax used to include the sniff.
|
||||
*
|
||||
* @param string $key Expected array key.
|
||||
* @param string $value Expected array value.
|
||||
*
|
||||
* @dataProvider dataRegisteredSniffCodes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRegisteredSniffCodes($key, $value)
|
||||
{
|
||||
$this->assertArrayHasKey($key, self::$ruleset->sniffCodes);
|
||||
$this->assertSame($value, self::$ruleset->sniffCodes[$key]);
|
||||
|
||||
}//end testRegisteredSniffCodes()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see self::testRegisteredSniffCodes()
|
||||
*
|
||||
* @return array<array<string>>
|
||||
*/
|
||||
public static function dataRegisteredSniffCodes()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'PSR2.Classes.ClassDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Classes.PropertyDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\PropertyDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.ControlStructures.ControlStructureSpacing',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.ControlStructures.ElseIfDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ElseIfDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.ControlStructures.SwitchDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\SwitchDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Files.ClosingTag',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\ClosingTagSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Files.EndFileNewline',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\EndFileNewlineSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Methods.FunctionCallSignature',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionCallSignatureSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Methods.FunctionClosingBrace',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionClosingBraceSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Methods.MethodDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\MethodDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Namespaces.NamespaceDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Namespaces\NamespaceDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR2.Namespaces.UseDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR2\Sniffs\Namespaces\UseDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR1.Classes.ClassDeclaration',
|
||||
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'PSR1.Files.SideEffects',
|
||||
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff',
|
||||
],
|
||||
[
|
||||
'PSR1.Methods.CamelCapsMethodName',
|
||||
'PHP_CodeSniffer\Standards\PSR1\Sniffs\Methods\CamelCapsMethodNameSniff',
|
||||
],
|
||||
[
|
||||
'Generic.PHP.DisallowAlternativePHPTags',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowAlternativePHPTagsSniff',
|
||||
],
|
||||
[
|
||||
'Generic.PHP.DisallowShortOpenTag',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowShortOpenTagSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Files.ByteOrderMark',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\ByteOrderMarkSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Classes.ValidClassName',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff',
|
||||
],
|
||||
[
|
||||
'Generic.NamingConventions.UpperCaseConstantName',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\UpperCaseConstantNameSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Files.LineEndings',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineEndingsSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Files.LineLength',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.WhiteSpace.SuperfluousWhitespace',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Formatting.DisallowMultipleStatements',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\DisallowMultipleStatementsSniff',
|
||||
],
|
||||
[
|
||||
'Generic.WhiteSpace.ScopeIndent',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff',
|
||||
],
|
||||
[
|
||||
'Generic.WhiteSpace.DisallowTabIndent',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\DisallowTabIndentSniff',
|
||||
],
|
||||
[
|
||||
'Generic.PHP.LowerCaseKeyword',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseKeywordSniff',
|
||||
],
|
||||
[
|
||||
'Generic.PHP.LowerCaseConstant',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseConstantSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Scope.MethodScope',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Scope\MethodScopeSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.WhiteSpace.ScopeKeywordSpacing',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\ScopeKeywordSpacingSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Functions.FunctionDeclaration',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\FunctionDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Functions.LowercaseFunctionKeywords',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\LowercaseFunctionKeywordsSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Functions.FunctionDeclarationArgumentSpacing',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\FunctionDeclarationArgumentSpacingSniff',
|
||||
],
|
||||
[
|
||||
'PEAR.Functions.ValidDefaultValue',
|
||||
'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.Functions.MultiLineFunctionDeclaration',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Functions.FunctionCallArgumentSpacing',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.ControlStructures.ControlSignature',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ControlSignatureSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.WhiteSpace.ControlStructureSpacing',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\ControlStructureSpacingSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.WhiteSpace.ScopeClosingBrace',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\ScopeClosingBraceSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.ControlStructures.ForEachLoopDeclaration',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ForEachLoopDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.ControlStructures.ForLoopDeclaration',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ForLoopDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'Squiz.ControlStructures.LowercaseDeclaration',
|
||||
'PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\LowercaseDeclarationSniff',
|
||||
],
|
||||
[
|
||||
'Generic.ControlStructures.InlineControlStructure',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures\InlineControlStructureSniff',
|
||||
],
|
||||
[
|
||||
'PSR12.Operators.OperatorSpacing',
|
||||
'PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Arrays.ArrayIndent',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Metrics.CyclomaticComplexity',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\CyclomaticComplexitySniff',
|
||||
],
|
||||
[
|
||||
'Generic.NamingConventions.CamelCapsFunctionName',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff',
|
||||
],
|
||||
[
|
||||
'Generic.Metrics.NestingLevel',
|
||||
'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataRegisteredSniffCodes()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting properties for standards, categories, sniffs works for all supported rule
|
||||
* inclusion methods.
|
||||
*
|
||||
* @param string $sniffClass The name of the sniff class.
|
||||
* @param string $propertyName The name of the changed property.
|
||||
* @param string|int|bool $expectedValue The value expected for the property.
|
||||
*
|
||||
* @dataProvider dataSettingProperties
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSettingProperties($sniffClass, $propertyName, $expectedValue)
|
||||
{
|
||||
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs);
|
||||
|
||||
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
|
||||
$errorMsg = sprintf('Property %s does not exist on sniff class %s', $propertyName, $sniffClass);
|
||||
$this->assertTrue($hasProperty, $errorMsg);
|
||||
|
||||
$actualValue = self::$ruleset->sniffs[$sniffClass]->$propertyName;
|
||||
$this->assertSame($expectedValue, $actualValue);
|
||||
|
||||
}//end testSettingProperties()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see self::testSettingProperties()
|
||||
*
|
||||
* @return array<string, array<string, string|int|bool>>
|
||||
*/
|
||||
public static function dataSettingProperties()
|
||||
{
|
||||
return [
|
||||
'Set property for complete standard: PSR2 ClassDeclaration' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff',
|
||||
'propertyName' => 'indent',
|
||||
'expectedValue' => '20',
|
||||
],
|
||||
'Set property for complete standard: PSR2 SwitchDeclaration' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\SwitchDeclarationSniff',
|
||||
'propertyName' => 'indent',
|
||||
'expectedValue' => '20',
|
||||
],
|
||||
'Set property for complete standard: PSR2 FunctionCallSignature' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionCallSignatureSniff',
|
||||
'propertyName' => 'indent',
|
||||
'expectedValue' => '20',
|
||||
],
|
||||
'Set property for complete category: PSR12 OperatorSpacing' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff',
|
||||
'propertyName' => 'ignoreSpacingBeforeAssignments',
|
||||
'expectedValue' => false,
|
||||
],
|
||||
'Set property for individual sniff: Generic ArrayIndent' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\ArrayIndentSniff',
|
||||
'propertyName' => 'indent',
|
||||
'expectedValue' => '2',
|
||||
],
|
||||
'Set property for individual sniff using sniff file inclusion: Generic LineLength' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff',
|
||||
'propertyName' => 'lineLimit',
|
||||
'expectedValue' => '10',
|
||||
],
|
||||
'Set property for individual sniff using sniff file inclusion: CamelCapsFunctionName' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff',
|
||||
'propertyName' => 'strict',
|
||||
'expectedValue' => false,
|
||||
],
|
||||
'Set property for individual sniff via included ruleset: NestingLevel - nestingLevel' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
|
||||
'propertyName' => 'nestingLevel',
|
||||
'expectedValue' => '2',
|
||||
],
|
||||
'Set property for all sniffs in an included ruleset: NestingLevel - absoluteNestingLevel' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\NestingLevelSniff',
|
||||
'propertyName' => 'absoluteNestingLevel',
|
||||
'expectedValue' => true,
|
||||
],
|
||||
|
||||
// Testing that setting a property at error code level does *not* work.
|
||||
'Set property for error code will not change the sniff property value: CyclomaticComplexity' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics\CyclomaticComplexitySniff',
|
||||
'propertyName' => 'complexity',
|
||||
'expectedValue' => 10,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataSettingProperties()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting properties for standards, categories on sniffs which don't support the property will
|
||||
* silently ignore the property and not set it.
|
||||
*
|
||||
* @param string $sniffClass The name of the sniff class.
|
||||
* @param string $propertyName The name of the property which should not be set.
|
||||
*
|
||||
* @dataProvider dataSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails($sniffClass, $propertyName)
|
||||
{
|
||||
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
|
||||
|
||||
$sniffObject = self::$ruleset->sniffs[$sniffClass];
|
||||
|
||||
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
|
||||
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
|
||||
$this->assertFalse($hasProperty, $errorMsg);
|
||||
|
||||
}//end testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see self::testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
|
||||
*
|
||||
* @return array<string, array>string, string>>
|
||||
*/
|
||||
public static function dataSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
|
||||
{
|
||||
return [
|
||||
'Set property for complete standard: PSR2 ClassDeclaration' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff',
|
||||
'propertyName' => 'setforallsniffs',
|
||||
],
|
||||
'Set property for complete standard: PSR2 FunctionCallSignature' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionCallSignatureSniff',
|
||||
'propertyName' => 'setforallsniffs',
|
||||
],
|
||||
'Set property for complete category: PSR12 OperatorSpacing' => [
|
||||
'sniffClass' => 'PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff',
|
||||
'propertyName' => 'setforallincategory',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFails()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-49
@@ -1,49 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="PSR2">
|
||||
<properties>
|
||||
<property name="setforallsniffs" value="true" />
|
||||
<property name="indent" value="20" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="PSR12.Operators">
|
||||
<properties>
|
||||
<property name="setforallincategory" value="true" />
|
||||
<property name="ignoreSpacingBeforeAssignments" value="false" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="Generic.Arrays.ArrayIndent">
|
||||
<properties>
|
||||
<property name="indent" value="2" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="Generic.Metrics.CyclomaticComplexity.MaxExceeded">
|
||||
<properties>
|
||||
<property name="complexity" value="50" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="./src/Standards/Generic/Sniffs/Files/LineLengthSniff.php">
|
||||
<properties>
|
||||
<property name="lineLimit" value="10" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="./../%path_root_dir%/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php">
|
||||
<properties>
|
||||
<property name="strict" value="false" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<rule ref="./RuleInclusionTest-include.xml">
|
||||
<!-- Property being set for all sniffs included in this ruleset. -->
|
||||
<properties>
|
||||
<property name="absoluteNestingLevel" value="true" />
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="./tests/Core/Ruleset/Fixtures/Sniffs/SetProperty/AllowedAsDeclaredSniff.php">
|
||||
<properties>
|
||||
<property name="arbitrarystring" value="arbitraryvalue"/>
|
||||
<property name="arbitraryarray" type="array">
|
||||
<element key="mykey" value="myvalue"/>
|
||||
</property>
|
||||
<property name="arbitraryarray" type="array" extend="true">
|
||||
<element key="otherkey" value="othervalue"/>
|
||||
</property>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="./tests/Core/Ruleset/Fixtures/Sniffs/SetProperty/AllowedViaMagicMethodSniff.php">
|
||||
<properties>
|
||||
<property name="arbitrarystring" value="arbitraryvalue"/>
|
||||
<property name="arbitraryarray" type="array">
|
||||
<element key="mykey" value="myvalue"/>
|
||||
</property>
|
||||
<property name="arbitraryarray" type="array" extend="true">
|
||||
<element key="otherkey" value="othervalue"/>
|
||||
</property>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="./tests/Core/Ruleset/Fixtures/Sniffs/SetProperty/AllowedViaStdClassSniff.php">
|
||||
<properties>
|
||||
<property name="arbitrarystring" value="arbitraryvalue"/>
|
||||
<property name="arbitraryarray" type="array">
|
||||
<element key="mykey" value="myvalue"/>
|
||||
</property>
|
||||
<property name="arbitraryarray" type="array" extend="true">
|
||||
<element key="otherkey" value="othervalue"/>
|
||||
</property>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="PEAR.Functions">
|
||||
<properties>
|
||||
<property name="indent" value="10"/>
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="Generic.Arrays">
|
||||
<properties>
|
||||
<property name="doesnotexist" value="2"/>
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="Generic">
|
||||
<properties>
|
||||
<property name="doesnotexist" value="2"/>
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="./tests/Core/Ruleset/Fixtures/Sniffs/SetProperty/NotAllowedViaAttributeSniff.php">
|
||||
<properties>
|
||||
<property name="arbitrarystring" value="arbitraryvalue"/>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Fixtures" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<rule ref="Generic.Arrays.ArrayIndent">
|
||||
<properties>
|
||||
<property name="indentation" value="2"/>
|
||||
</properties>
|
||||
</rule>
|
||||
</ruleset>
|
||||
Vendored
-405
@@ -1,405 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the handling of properties being set via the ruleset.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2022 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionObject;
|
||||
|
||||
/**
|
||||
* These tests specifically focus on the changes made to work around the PHP 8.2 dynamic properties deprecation.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset::setSniffProperty
|
||||
*/
|
||||
final class SetSniffPropertyTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting a property via the ruleset works in all situations which allow for it.
|
||||
*
|
||||
* @param string $name Name of the test. Used for the sniff name, the ruleset file name etc.
|
||||
*
|
||||
* @dataProvider dataSniffPropertiesGetSetWhenAllowed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSniffPropertiesGetSetWhenAllowed($name)
|
||||
{
|
||||
$sniffCode = "Fixtures.SetProperty.{$name}";
|
||||
$sniffClass = 'Fixtures\Sniffs\SetProperty\\'.$name.'Sniff';
|
||||
$properties = [
|
||||
'arbitrarystring' => 'arbitraryvalue',
|
||||
'arbitraryarray' => [
|
||||
'mykey' => 'myvalue',
|
||||
'otherkey' => 'othervalue',
|
||||
],
|
||||
];
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__."/SetProperty{$name}Test.xml";
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
// Verify that the sniff has been registered.
|
||||
$this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered');
|
||||
|
||||
// Verify that our target sniff has been registered.
|
||||
$this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, 'Target sniff not registered');
|
||||
$this->assertSame($sniffClass, $ruleset->sniffCodes[$sniffCode], 'Target sniff not registered with the correct class');
|
||||
|
||||
// Test that the property as declared in the ruleset has been set on the sniff.
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class not listed in registered sniffs');
|
||||
|
||||
$sniffObject = $ruleset->sniffs[$sniffClass];
|
||||
foreach ($properties as $name => $expectedValue) {
|
||||
$this->assertSame($expectedValue, $sniffObject->$name, 'Property value not set to expected value');
|
||||
}
|
||||
|
||||
}//end testSniffPropertiesGetSetWhenAllowed()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see self::testSniffPropertiesGetSetWhenAllowed()
|
||||
*
|
||||
* @return array<string, array<string>>
|
||||
*/
|
||||
public static function dataSniffPropertiesGetSetWhenAllowed()
|
||||
{
|
||||
return [
|
||||
'Property allowed as explicitly declared' => ['AllowedAsDeclared'],
|
||||
'Property allowed as sniff extends stdClass' => ['AllowedViaStdClass'],
|
||||
'Property allowed as sniff has magic __set() method' => ['AllowedViaMagicMethod'],
|
||||
];
|
||||
|
||||
}//end dataSniffPropertiesGetSetWhenAllowed()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting a property for a category will apply it correctly to those sniffs which support the
|
||||
* property, but won't apply it to sniffs which don't.
|
||||
*
|
||||
* Note: this test intentionally uses the `PEAR.Functions` category as two sniffs in that category
|
||||
* have a public property with the same name (`indent`) and one sniff doesn't, which makes it a great
|
||||
* test case for this.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
|
||||
{
|
||||
$propertyName = 'indent';
|
||||
$expectedValue = '10';
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/SetPropertyAppliesPropertyToMultipleSniffsInCategoryTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
// Test that the two sniffs which support the property have received the value.
|
||||
$sniffClass = 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff';
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
|
||||
$sniffObject = $ruleset->sniffs[$sniffClass];
|
||||
$this->assertSame($expectedValue, $sniffObject->$propertyName, 'Property value not set to expected value for '.$sniffClass);
|
||||
|
||||
$sniffClass = 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionDeclarationSniff';
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
|
||||
$sniffObject = $ruleset->sniffs[$sniffClass];
|
||||
$this->assertSame($expectedValue, $sniffObject->$propertyName, 'Property value not set to expected value for '.$sniffClass);
|
||||
|
||||
// Test that the property doesn't get set for the one sniff which doesn't support the property.
|
||||
$sniffClass = 'PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\ValidDefaultValueSniff';
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
|
||||
|
||||
$hasProperty = (new ReflectionObject($ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
|
||||
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
|
||||
$this->assertFalse($hasProperty, $errorMsg);
|
||||
|
||||
}//end testSetPropertyAppliesPropertyToMultipleSniffsInCategory()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attempting to set a non-existent property directly on a sniff will throw an error
|
||||
* when the sniff does not explicitly declare the property, extends stdClass or has magic methods.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetPropertyThrowsErrorOnInvalidProperty()
|
||||
{
|
||||
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
|
||||
$exceptionMsg = 'Ruleset invalid. Property "indentation" does not exist on sniff Generic.Arrays.ArrayIndent';
|
||||
if (method_exists($this, 'expectException') === true) {
|
||||
$this->expectException($exceptionClass);
|
||||
$this->expectExceptionMessage($exceptionMsg);
|
||||
} else {
|
||||
// PHPUnit < 5.2.0.
|
||||
$this->setExpectedException($exceptionClass, $exceptionMsg);
|
||||
}
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
}//end testSetPropertyThrowsErrorOnInvalidProperty()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attempting to set a non-existent property directly on a sniff will throw an error
|
||||
* when the sniff does not explicitly declare the property, extends stdClass or has magic methods,
|
||||
* even though the sniff has the PHP 8.2 `#[AllowDynamicProperties]` attribute set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
|
||||
{
|
||||
$exceptionClass = 'PHP_CodeSniffer\Exceptions\RuntimeException';
|
||||
$exceptionMsg = 'Ruleset invalid. Property "arbitrarystring" does not exist on sniff Fixtures.SetProperty.NotAllowedViaAttribute';
|
||||
if (method_exists($this, 'expectException') === true) {
|
||||
$this->expectException($exceptionClass);
|
||||
$this->expectExceptionMessage($exceptionMsg);
|
||||
} else {
|
||||
// PHPUnit < 5.2.0.
|
||||
$this->setExpectedException($exceptionClass, $exceptionMsg);
|
||||
}
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
}//end testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attempting to set a non-existent property on a sniff when the property directive is
|
||||
* for the whole standard, does not yield an error.
|
||||
*
|
||||
* @doesNotPerformAssertions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandard()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandard()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attempting to set a non-existent property on a sniff when the property directive is
|
||||
* for a whole category, does not yield an error.
|
||||
*
|
||||
* @doesNotPerformAssertions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
|
||||
* sets the property correctly when using the new $settings array format.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDirectCallWithNewArrayFormatSetsProperty()
|
||||
{
|
||||
$name = 'AllowedAsDeclared';
|
||||
$sniffCode = "Fixtures.SetProperty.{$name}";
|
||||
$sniffClass = 'Fixtures\Sniffs\SetProperty\\'.$name.'Sniff';
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__."/SetProperty{$name}Test.xml";
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$propertyName = 'arbitrarystring';
|
||||
$propertyValue = 'new value';
|
||||
|
||||
$ruleset->setSniffProperty(
|
||||
$sniffClass,
|
||||
$propertyName,
|
||||
[
|
||||
'scope' => 'sniff',
|
||||
'value' => $propertyValue,
|
||||
]
|
||||
);
|
||||
|
||||
// Verify that the sniff has been registered.
|
||||
$this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered');
|
||||
|
||||
// Verify that our target sniff has been registered.
|
||||
$this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, 'Target sniff not registered');
|
||||
$this->assertSame($sniffClass, $ruleset->sniffCodes[$sniffCode], 'Target sniff not registered with the correct class');
|
||||
|
||||
// Test that the property as declared in the ruleset has been set on the sniff.
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class not listed in registered sniffs');
|
||||
|
||||
$sniffObject = $ruleset->sniffs[$sniffClass];
|
||||
$this->assertSame($propertyValue, $sniffObject->$propertyName, 'Property value not set to expected value');
|
||||
|
||||
}//end testDirectCallWithNewArrayFormatSetsProperty()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
|
||||
* sets the property correctly when using the old $settings array format.
|
||||
*
|
||||
* Tested by silencing the deprecation notice as otherwise the test would fail on the deprecation notice.
|
||||
*
|
||||
* @param mixed $propertyValue Value for the property to set.
|
||||
*
|
||||
* @dataProvider dataDirectCallWithOldArrayFormatSetsProperty
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDirectCallWithOldArrayFormatSetsProperty($propertyValue)
|
||||
{
|
||||
$name = 'AllowedAsDeclared';
|
||||
$sniffCode = "Fixtures.SetProperty.{$name}";
|
||||
$sniffClass = 'Fixtures\Sniffs\SetProperty\\'.$name.'Sniff';
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__."/SetProperty{$name}Test.xml";
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$propertyName = 'arbitrarystring';
|
||||
|
||||
@$ruleset->setSniffProperty(
|
||||
$sniffClass,
|
||||
$propertyName,
|
||||
$propertyValue
|
||||
);
|
||||
|
||||
// Verify that the sniff has been registered.
|
||||
$this->assertGreaterThan(0, count($ruleset->sniffCodes), 'No sniff codes registered');
|
||||
|
||||
// Verify that our target sniff has been registered.
|
||||
$this->assertArrayHasKey($sniffCode, $ruleset->sniffCodes, 'Target sniff not registered');
|
||||
$this->assertSame($sniffClass, $ruleset->sniffCodes[$sniffCode], 'Target sniff not registered with the correct class');
|
||||
|
||||
// Test that the property as declared in the ruleset has been set on the sniff.
|
||||
$this->assertArrayHasKey($sniffClass, $ruleset->sniffs, 'Sniff class not listed in registered sniffs');
|
||||
|
||||
$sniffObject = $ruleset->sniffs[$sniffClass];
|
||||
$this->assertSame($propertyValue, $sniffObject->$propertyName, 'Property value not set to expected value');
|
||||
|
||||
}//end testDirectCallWithOldArrayFormatSetsProperty()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see self::testDirectCallWithOldArrayFormatSetsProperty()
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
public static function dataDirectCallWithOldArrayFormatSetsProperty()
|
||||
{
|
||||
return [
|
||||
'Property value is not an array (boolean)' => [
|
||||
'propertyValue' => false,
|
||||
],
|
||||
'Property value is not an array (string)' => [
|
||||
'propertyValue' => 'a string',
|
||||
],
|
||||
'Property value is an empty array' => [
|
||||
'propertyValue' => [],
|
||||
],
|
||||
'Property value is an array without keys' => [
|
||||
'propertyValue' => [
|
||||
'value',
|
||||
false,
|
||||
],
|
||||
],
|
||||
'Property value is an array without the "scope" or "value" keys' => [
|
||||
'propertyValue' => [
|
||||
'key1' => 'value',
|
||||
'key2' => false,
|
||||
],
|
||||
],
|
||||
'Property value is an array without the "scope" key' => [
|
||||
'propertyValue' => [
|
||||
'key1' => 'value',
|
||||
'value' => true,
|
||||
],
|
||||
],
|
||||
'Property value is an array without the "value" key' => [
|
||||
'propertyValue' => [
|
||||
'scope' => 'value',
|
||||
'key2' => 1234,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataDirectCallWithOldArrayFormatSetsProperty()
|
||||
|
||||
|
||||
/**
|
||||
* Test that setting a property via a direct call to the Ruleset::setSniffProperty() method
|
||||
* throws a deprecation notice when using the old $settings array format.
|
||||
*
|
||||
* Note: as PHPUnit stops as soon as it sees the deprecation notice, the setting of the property
|
||||
* value is not tested here.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDirectCallWithOldArrayFormatThrowsDeprecationNotice()
|
||||
{
|
||||
$exceptionClass = 'PHPUnit\Framework\Error\Deprecated';
|
||||
if (class_exists($exceptionClass) === false) {
|
||||
$exceptionClass = 'PHPUnit_Framework_Error_Deprecated';
|
||||
}
|
||||
|
||||
$exceptionMsg = 'the format of the $settings parameter has changed from (mixed) $value to array(\'scope\' => \'sniff|standard\', \'value\' => $value). Please update your integration code. See PR #3629 for more information.';
|
||||
|
||||
if (method_exists($this, 'expectException') === true) {
|
||||
$this->expectException($exceptionClass);
|
||||
$this->expectExceptionMessage($exceptionMsg);
|
||||
} else {
|
||||
// PHPUnit < 5.2.0.
|
||||
$this->setExpectedException($exceptionClass, $exceptionMsg);
|
||||
}
|
||||
|
||||
$name = 'AllowedAsDeclared';
|
||||
$sniffCode = "Fixtures.SetProperty.{$name}";
|
||||
$sniffClass = 'Fixtures\Sniffs\SetProperty\\'.$name.'Sniff';
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__."/SetProperty{$name}Test.xml";
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$propertyName = 'arbitrarystring';
|
||||
|
||||
$ruleset->setSniffProperty(
|
||||
$sniffClass,
|
||||
'arbitrarystring',
|
||||
['key' => 'value']
|
||||
);
|
||||
|
||||
}//end testDirectCallWithOldArrayFormatThrowsDeprecationNotice()
|
||||
|
||||
|
||||
}//end class
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.DeprecatedInvalid.EmptyDeprecationVersion"/>
|
||||
|
||||
</ruleset>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.DeprecatedInvalid.EmptyRemovalVersion"/>
|
||||
|
||||
</ruleset>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.DeprecatedInvalid.InvalidDeprecationMessage"/>
|
||||
|
||||
</ruleset>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.DeprecatedInvalid.InvalidDeprecationVersion"/>
|
||||
|
||||
</ruleset>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.DeprecatedInvalid.InvalidRemovalVersion"/>
|
||||
|
||||
</ruleset>
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<!-- This list is non-alphabetic on purpose. The display order is what is being tested. -->
|
||||
<rule ref="Fixtures.Deprecated.WithReplacement"/>
|
||||
<rule ref="Fixtures.Deprecated.WithoutReplacement"/>
|
||||
|
||||
</ruleset>
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures.Deprecated.WithLongReplacement"/>
|
||||
|
||||
</ruleset>
|
||||
-510
@@ -1,510 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests PHPCS native handling of sniff deprecations.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2024 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Tests PHPCS native handling of sniff deprecations.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Ruleset::hasSniffDeprecations
|
||||
* @covers \PHP_CodeSniffer\Ruleset::showSniffDeprecations
|
||||
*/
|
||||
final class ShowSniffDeprecationsTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test the return value of the hasSniffDeprecations() method.
|
||||
*
|
||||
* @param string $standard The standard to use for the test.
|
||||
* @param bool $expected The expected function return value.
|
||||
*
|
||||
* @dataProvider dataHasSniffDeprecations
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHasSniffDeprecations($standard, $expected)
|
||||
{
|
||||
$config = new ConfigDouble(['.', "--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$this->assertSame($expected, $ruleset->hasSniffDeprecations());
|
||||
|
||||
}//end testHasSniffDeprecations()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testHasSniffDeprecations()
|
||||
*
|
||||
* @return array<string, array<string, string|bool>>
|
||||
*/
|
||||
public static function dataHasSniffDeprecations()
|
||||
{
|
||||
return [
|
||||
'Standard not using deprecated sniffs: PSR1' => [
|
||||
'standard' => 'PSR1',
|
||||
'expected' => false,
|
||||
],
|
||||
'Standard using deprecated sniffs: Test Fixture' => [
|
||||
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
|
||||
'expected' => true,
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataHasSniffDeprecations()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the listing with deprecated sniffs will not show when specific command-line options are being used.
|
||||
*
|
||||
* @param string $standard The standard to use for the test.
|
||||
* @param array<string> $additionalArgs Optional. Additional arguments to pass.
|
||||
*
|
||||
* @dataProvider dataDeprecatedSniffsListDoesNotShow
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeprecatedSniffsListDoesNotShow($standard, $additionalArgs=[])
|
||||
{
|
||||
$args = $additionalArgs;
|
||||
$args[] = '.';
|
||||
$args[] = "--standard=$standard";
|
||||
|
||||
$config = new ConfigDouble($args);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$this->expectOutputString('');
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testDeprecatedSniffsListDoesNotShow()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testDeprecatedSniffsListDoesNotShow()
|
||||
*
|
||||
* @return array<string, array<string, string|array<string>>>
|
||||
*/
|
||||
public static function dataDeprecatedSniffsListDoesNotShow()
|
||||
{
|
||||
return [
|
||||
'Standard not using deprecated sniffs: PSR1' => [
|
||||
'standard' => 'PSR1',
|
||||
],
|
||||
'Standard using deprecated sniffs; explain mode' => [
|
||||
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
|
||||
'additionalArgs' => ['-e'],
|
||||
],
|
||||
'Standard using deprecated sniffs; quiet mode' => [
|
||||
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
|
||||
'additionalArgs' => ['-q'],
|
||||
],
|
||||
'Standard using deprecated sniffs; documentation is requested' => [
|
||||
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
|
||||
'additionalArgs' => ['--generator=text'],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataDeprecatedSniffsListDoesNotShow()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the listing with deprecated sniffs will not show when using a standard containing deprecated sniffs,
|
||||
* but only running select non-deprecated sniffs (using `--sniffs=...`).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeprecatedSniffsListDoesNotShowWhenSelectedSniffsAreNotDeprecated()
|
||||
{
|
||||
$standard = __DIR__.'/ShowSniffDeprecationsTest.xml';
|
||||
$config = new ConfigDouble(['.', "--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
/*
|
||||
* Apply sniff restrictions.
|
||||
* For tests we need to manually trigger this if the standard is "installed", like with the fixtures these tests use.
|
||||
*/
|
||||
|
||||
$restrictions = [];
|
||||
$sniffs = [
|
||||
'Fixtures.SetProperty.AllowedAsDeclared',
|
||||
'Fixtures.SetProperty.AllowedViaStdClass',
|
||||
];
|
||||
foreach ($sniffs as $sniffCode) {
|
||||
$parts = explode('.', strtolower($sniffCode));
|
||||
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
|
||||
$restrictions[strtolower($sniffName)] = true;
|
||||
}
|
||||
|
||||
$sniffFiles = [];
|
||||
$allSniffs = $ruleset->sniffCodes;
|
||||
foreach ($allSniffs as $sniffName) {
|
||||
$sniffFile = str_replace('\\', DIRECTORY_SEPARATOR, $sniffName);
|
||||
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.$sniffFile.'.php';
|
||||
$sniffFiles[] = $sniffFile;
|
||||
}
|
||||
|
||||
$ruleset->registerSniffs($allSniffs, $restrictions, []);
|
||||
$ruleset->populateTokenListeners();
|
||||
|
||||
$this->expectOutputString('');
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testDeprecatedSniffsListDoesNotShowWhenSelectedSniffsAreNotDeprecated()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the listing with deprecated sniffs will not show when using a standard containing deprecated sniffs,
|
||||
* but all deprecated sniffs have been excluded from the run (using `--exclude=...`).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeprecatedSniffsListDoesNotShowWhenAllDeprecatedSniffsAreExcluded()
|
||||
{
|
||||
$standard = __DIR__.'/ShowSniffDeprecationsTest.xml';
|
||||
$config = new ConfigDouble(['.', "--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
/*
|
||||
* Apply sniff restrictions.
|
||||
* For tests we need to manually trigger this if the standard is "installed", like with the fixtures these tests use.
|
||||
*/
|
||||
|
||||
$exclusions = [];
|
||||
$exclude = [
|
||||
'Fixtures.Deprecated.WithLongReplacement',
|
||||
'Fixtures.Deprecated.WithoutReplacement',
|
||||
'Fixtures.Deprecated.WithReplacement',
|
||||
'Fixtures.Deprecated.WithReplacementContainingLinuxNewlines',
|
||||
'Fixtures.Deprecated.WithReplacementContainingNewlines',
|
||||
];
|
||||
foreach ($exclude as $sniffCode) {
|
||||
$parts = explode('.', strtolower($sniffCode));
|
||||
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
|
||||
$exclusions[strtolower($sniffName)] = true;
|
||||
}
|
||||
|
||||
$sniffFiles = [];
|
||||
$allSniffs = $ruleset->sniffCodes;
|
||||
foreach ($allSniffs as $sniffName) {
|
||||
$sniffFile = str_replace('\\', DIRECTORY_SEPARATOR, $sniffName);
|
||||
$sniffFile = __DIR__.DIRECTORY_SEPARATOR.$sniffFile.'.php';
|
||||
$sniffFiles[] = $sniffFile;
|
||||
}
|
||||
|
||||
$ruleset->registerSniffs($allSniffs, [], $exclusions);
|
||||
$ruleset->populateTokenListeners();
|
||||
|
||||
$this->expectOutputString('');
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testDeprecatedSniffsListDoesNotShowWhenAllDeprecatedSniffsAreExcluded()
|
||||
|
||||
|
||||
/**
|
||||
* Test deprecated sniffs are listed alphabetically in the deprecated sniffs warning.
|
||||
*
|
||||
* This tests a number of different aspects:
|
||||
* 1. That the summary line uses the correct grammar when there is are multiple deprecated sniffs.
|
||||
* 2. That there is no trailing whitespace when the sniff does not provide a custom message.
|
||||
* 3. That custom messages containing new line characters (any type) are handled correctly and
|
||||
* that those new line characters are converted to the OS supported new line char.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeprecatedSniffsWarning()
|
||||
{
|
||||
$standard = __DIR__.'/ShowSniffDeprecationsTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard", '--no-colors']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = 'WARNING: The SniffDeprecationTest standard uses 5 deprecated sniffs'.PHP_EOL;
|
||||
$expected .= '--------------------------------------------------------------------------------'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithLongReplacement'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel'.PHP_EOL;
|
||||
$expected .= ' vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed.'.PHP_EOL;
|
||||
$expected .= ' Fusce egestas congue massa semper cursus. Donec quis pretium tellus. In'.PHP_EOL;
|
||||
$expected .= ' lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan'.PHP_EOL;
|
||||
$expected .= ' eros sapien at sem. Sed pulvinar aliquam malesuada. Aliquam erat volutpat.'.PHP_EOL;
|
||||
$expected .= ' Mauris gravida rutrum lectus at egestas. Fusce tempus elit in tincidunt'.PHP_EOL;
|
||||
$expected .= ' dictum. Suspendisse dictum egestas sapien, eget ullamcorper metus elementum'.PHP_EOL;
|
||||
$expected .= ' semper. Vestibulum sem justo, consectetur ac tincidunt et, finibus eget'.PHP_EOL;
|
||||
$expected .= ' libero.'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithoutReplacement'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.4.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithReplacement'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= ' Use the Stnd.Category.OtherSniff sniff instead.'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithReplacementContainingLinuxNewlines'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= ' Lorem ipsum dolor sit amet, consectetur adipiscing elit.'.PHP_EOL;
|
||||
$expected .= ' Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium'.PHP_EOL;
|
||||
$expected .= ' sed.'.PHP_EOL;
|
||||
$expected .= ' Fusce egestas congue massa semper cursus. Donec quis pretium tellus.'.PHP_EOL;
|
||||
$expected .= ' In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan'.PHP_EOL;
|
||||
$expected .= ' eros sapien at sem.'.PHP_EOL;
|
||||
$expected .= ' Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum'.PHP_EOL;
|
||||
$expected .= ' lectus at egestas.'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithReplacementContainingNewlines'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= ' Lorem ipsum dolor sit amet, consectetur adipiscing elit.'.PHP_EOL;
|
||||
$expected .= ' Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium'.PHP_EOL;
|
||||
$expected .= ' sed.'.PHP_EOL;
|
||||
$expected .= ' Fusce egestas congue massa semper cursus. Donec quis pretium tellus.'.PHP_EOL;
|
||||
$expected .= ' In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan'.PHP_EOL;
|
||||
$expected .= ' eros sapien at sem.'.PHP_EOL;
|
||||
$expected .= ' Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum'.PHP_EOL;
|
||||
$expected .= ' lectus at egestas'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Deprecated sniffs are still run, but will stop working at some point in the'.PHP_EOL;
|
||||
$expected .= 'future.'.PHP_EOL.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testDeprecatedSniffsWarning()
|
||||
|
||||
|
||||
/**
|
||||
* Test deprecated sniffs are listed alphabetically in the deprecated sniffs warning.
|
||||
*
|
||||
* This tests the following aspects:
|
||||
* 1. That the summary line uses the correct grammar when there is a single deprecated sniff.
|
||||
* 2. That the separator line below the summary maximizes at the longest line length.
|
||||
* 3. That the word wrapping respects the maximum report width.
|
||||
* 4. That the sniff name is truncated if it is longer than the max report width.
|
||||
*
|
||||
* @param int $reportWidth Report width for the test.
|
||||
* @param string $expectedOutput Expected output.
|
||||
*
|
||||
* @dataProvider dataReportWidthIsRespected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReportWidthIsRespected($reportWidth, $expectedOutput)
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/ShowSniffDeprecationsReportWidthTest.xml';
|
||||
$config = new ConfigDouble(['.', "--standard=$standard", "--report-width=$reportWidth", '--no-colors']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$this->expectOutputString($expectedOutput);
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testReportWidthIsRespected()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testReportWidthIsRespected()
|
||||
*
|
||||
* @return array<string, array<string, int|string>>
|
||||
*/
|
||||
public static function dataReportWidthIsRespected()
|
||||
{
|
||||
$summaryLine = 'WARNING: The SniffDeprecationTest standard uses 1 deprecated sniff'.PHP_EOL;
|
||||
|
||||
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Test readability is more important.
|
||||
return [
|
||||
'Report width small: 40; with truncated sniff name and wrapped header and footer lines' => [
|
||||
'reportWidth' => 40,
|
||||
'expectedOutput' => 'WARNING: The SniffDeprecationTest'.PHP_EOL
|
||||
.'standard uses 1 deprecated sniff'.PHP_EOL
|
||||
.'----------------------------------------'.PHP_EOL
|
||||
.'- Fixtures.Deprecated.WithLongRepla...'.PHP_EOL
|
||||
.' This sniff has been deprecated since'.PHP_EOL
|
||||
.' v3.8.0 and will be removed in'.PHP_EOL
|
||||
.' v4.0.0. Lorem ipsum dolor sit amet,'.PHP_EOL
|
||||
.' consectetur adipiscing elit. Fusce'.PHP_EOL
|
||||
.' vel vestibulum nunc. Sed luctus'.PHP_EOL
|
||||
.' dolor tortor, eu euismod purus'.PHP_EOL
|
||||
.' pretium sed. Fusce egestas congue'.PHP_EOL
|
||||
.' massa semper cursus. Donec quis'.PHP_EOL
|
||||
.' pretium tellus. In lacinia, augue ut'.PHP_EOL
|
||||
.' ornare porttitor, diam nunc faucibus'.PHP_EOL
|
||||
.' purus, et accumsan eros sapien at'.PHP_EOL
|
||||
.' sem. Sed pulvinar aliquam malesuada.'.PHP_EOL
|
||||
.' Aliquam erat volutpat. Mauris'.PHP_EOL
|
||||
.' gravida rutrum lectus at egestas.'.PHP_EOL
|
||||
.' Fusce tempus elit in tincidunt'.PHP_EOL
|
||||
.' dictum. Suspendisse dictum egestas'.PHP_EOL
|
||||
.' sapien, eget ullamcorper metus'.PHP_EOL
|
||||
.' elementum semper. Vestibulum sem'.PHP_EOL
|
||||
.' justo, consectetur ac tincidunt et,'.PHP_EOL
|
||||
.' finibus eget libero.'.PHP_EOL.PHP_EOL
|
||||
.'Deprecated sniffs are still run, but'.PHP_EOL
|
||||
.'will stop working at some point in the'.PHP_EOL
|
||||
.'future.'.PHP_EOL.PHP_EOL,
|
||||
],
|
||||
'Report width default: 80' => [
|
||||
'reportWidth' => 80,
|
||||
'expectedOutput' => $summaryLine.str_repeat('-', 80).PHP_EOL
|
||||
.'- Fixtures.Deprecated.WithLongReplacement'.PHP_EOL
|
||||
.' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL
|
||||
.' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel'.PHP_EOL
|
||||
.' vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed.'.PHP_EOL
|
||||
.' Fusce egestas congue massa semper cursus. Donec quis pretium tellus. In'.PHP_EOL
|
||||
.' lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan'.PHP_EOL
|
||||
.' eros sapien at sem. Sed pulvinar aliquam malesuada. Aliquam erat volutpat.'.PHP_EOL
|
||||
.' Mauris gravida rutrum lectus at egestas. Fusce tempus elit in tincidunt'.PHP_EOL
|
||||
.' dictum. Suspendisse dictum egestas sapien, eget ullamcorper metus elementum'.PHP_EOL
|
||||
.' semper. Vestibulum sem justo, consectetur ac tincidunt et, finibus eget'.PHP_EOL
|
||||
.' libero.'.PHP_EOL.PHP_EOL
|
||||
.'Deprecated sniffs are still run, but will stop working at some point in the'.PHP_EOL
|
||||
.'future.'.PHP_EOL.PHP_EOL,
|
||||
],
|
||||
'Report width matches longest line: 666; the message should not wrap' => [
|
||||
// Length = 4 padding + 75 base line + 587 custom message.
|
||||
'reportWidth' => 666,
|
||||
'expectedOutput' => $summaryLine.str_repeat('-', 666).PHP_EOL
|
||||
.'- Fixtures.Deprecated.WithLongReplacement'.PHP_EOL
|
||||
.' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed. Fusce egestas congue massa semper cursus. Donec quis pretium tellus. In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan eros sapien at sem. Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum lectus at egestas. Fusce tempus elit in tincidunt dictum. Suspendisse dictum egestas sapien, eget ullamcorper metus elementum semper. Vestibulum sem justo, consectetur ac tincidunt et, finibus eget libero.'
|
||||
.PHP_EOL.PHP_EOL
|
||||
.'Deprecated sniffs are still run, but will stop working at some point in the future.'.PHP_EOL.PHP_EOL,
|
||||
],
|
||||
'Report width wide: 1000; delimiter line length should match longest line' => [
|
||||
'reportWidth' => 1000,
|
||||
'expectedOutput' => $summaryLine.str_repeat('-', 666).PHP_EOL
|
||||
.'- Fixtures.Deprecated.WithLongReplacement'.PHP_EOL
|
||||
.' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vel vestibulum nunc. Sed luctus dolor tortor, eu euismod purus pretium sed. Fusce egestas congue massa semper cursus. Donec quis pretium tellus. In lacinia, augue ut ornare porttitor, diam nunc faucibus purus, et accumsan eros sapien at sem. Sed pulvinar aliquam malesuada. Aliquam erat volutpat. Mauris gravida rutrum lectus at egestas. Fusce tempus elit in tincidunt dictum. Suspendisse dictum egestas sapien, eget ullamcorper metus elementum semper. Vestibulum sem justo, consectetur ac tincidunt et, finibus eget libero.'
|
||||
.PHP_EOL.PHP_EOL
|
||||
.'Deprecated sniffs are still run, but will stop working at some point in the future.'.PHP_EOL.PHP_EOL,
|
||||
],
|
||||
];
|
||||
// phpcs:enable
|
||||
|
||||
}//end dataReportWidthIsRespected()
|
||||
|
||||
|
||||
/**
|
||||
* Test deprecated sniffs are listed alphabetically in the deprecated sniffs warning.
|
||||
*
|
||||
* Additionally, this test verifies that deprecated sniffs are still registered to run.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDeprecatedSniffsAreListedAlphabetically()
|
||||
{
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/ShowSniffDeprecationsOrderTest.xml';
|
||||
$config = new ConfigDouble(["--standard=$standard", '--no-colors']);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$expected = 'WARNING: The SniffDeprecationTest standard uses 2 deprecated sniffs'.PHP_EOL;
|
||||
$expected .= '--------------------------------------------------------------------------------'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithoutReplacement'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.4.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= '- Fixtures.Deprecated.WithReplacement'.PHP_EOL;
|
||||
$expected .= ' This sniff has been deprecated since v3.8.0 and will be removed in v4.0.0.'.PHP_EOL;
|
||||
$expected .= ' Use the Stnd.Category.OtherSniff sniff instead.'.PHP_EOL.PHP_EOL;
|
||||
$expected .= 'Deprecated sniffs are still run, but will stop working at some point in the'.PHP_EOL;
|
||||
$expected .= 'future.'.PHP_EOL.PHP_EOL;
|
||||
|
||||
$this->expectOutputString($expected);
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
// Verify that the sniffs have been registered to run.
|
||||
$this->assertCount(2, $ruleset->sniffCodes, 'Incorrect number of sniff codes registered');
|
||||
$this->assertArrayHasKey(
|
||||
'Fixtures.Deprecated.WithoutReplacement',
|
||||
$ruleset->sniffCodes,
|
||||
'WithoutReplacement sniff not registered'
|
||||
);
|
||||
$this->assertArrayHasKey(
|
||||
'Fixtures.Deprecated.WithReplacement',
|
||||
$ruleset->sniffCodes,
|
||||
'WithReplacement sniff not registered'
|
||||
);
|
||||
|
||||
}//end testDeprecatedSniffsAreListedAlphabetically()
|
||||
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown when any of the interface required methods does not
|
||||
* comply with the return type/value requirements.
|
||||
*
|
||||
* @param string $standard The standard to use for the test.
|
||||
* @param string $exceptionMessage The contents of the expected exception message.
|
||||
*
|
||||
* @dataProvider dataExceptionIsThrownOnIncorrectlyImplementedInterface
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testExceptionIsThrownOnIncorrectlyImplementedInterface($standard, $exceptionMessage)
|
||||
{
|
||||
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';
|
||||
if (method_exists($this, 'expectException') === true) {
|
||||
// PHPUnit 5+.
|
||||
$this->expectException($exception);
|
||||
$this->expectExceptionMessage($exceptionMessage);
|
||||
} else {
|
||||
// PHPUnit 4.
|
||||
$this->setExpectedException($exception, $exceptionMessage);
|
||||
}
|
||||
|
||||
// Set up the ruleset.
|
||||
$standard = __DIR__.'/'.$standard;
|
||||
$config = new ConfigDouble(["--standard=$standard"]);
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
$ruleset->showSniffDeprecations();
|
||||
|
||||
}//end testExceptionIsThrownOnIncorrectlyImplementedInterface()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testExceptionIsThrownOnIncorrectlyImplementedInterface()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataExceptionIsThrownOnIncorrectlyImplementedInterface()
|
||||
{
|
||||
return [
|
||||
'getDeprecationVersion() does not return a string' => [
|
||||
'standard' => 'ShowSniffDeprecationsInvalidDeprecationVersionTest.xml',
|
||||
'exceptionMessage' => 'The Fixtures\Sniffs\DeprecatedInvalid\InvalidDeprecationVersionSniff::getDeprecationVersion() method must return a non-empty string, received double',
|
||||
],
|
||||
'getRemovalVersion() does not return a string' => [
|
||||
'standard' => 'ShowSniffDeprecationsInvalidRemovalVersionTest.xml',
|
||||
'exceptionMessage' => 'The Fixtures\Sniffs\DeprecatedInvalid\InvalidRemovalVersionSniff::getRemovalVersion() method must return a non-empty string, received array',
|
||||
],
|
||||
'getDeprecationMessage() does not return a string' => [
|
||||
'standard' => 'ShowSniffDeprecationsInvalidDeprecationMessageTest.xml',
|
||||
'exceptionMessage' => 'The Fixtures\Sniffs\DeprecatedInvalid\InvalidDeprecationMessageSniff::getDeprecationMessage() method must return a string, received object',
|
||||
],
|
||||
'getDeprecationVersion() returns an empty string' => [
|
||||
'standard' => 'ShowSniffDeprecationsEmptyDeprecationVersionTest.xml',
|
||||
'exceptionMessage' => 'The Fixtures\Sniffs\DeprecatedInvalid\EmptyDeprecationVersionSniff::getDeprecationVersion() method must return a non-empty string, received ""',
|
||||
],
|
||||
'getRemovalVersion() returns an empty string' => [
|
||||
'standard' => 'ShowSniffDeprecationsEmptyRemovalVersionTest.xml',
|
||||
'exceptionMessage' => 'The Fixtures\Sniffs\DeprecatedInvalid\EmptyRemovalVersionSniff::getRemovalVersion() method must return a non-empty string, received ""',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataExceptionIsThrownOnIncorrectlyImplementedInterface()
|
||||
|
||||
|
||||
}//end class
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="SniffDeprecationTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
|
||||
|
||||
<config name="installed_paths" value="./tests/Core/Ruleset/Fixtures/"/>
|
||||
|
||||
<rule ref="Fixtures">
|
||||
<exclude name="Fixtures.DeprecatedInvalid"/>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
||||
Vendored
-51
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testSimpleValues */
|
||||
$foo = [1,2,3];
|
||||
|
||||
/* testSimpleKeyValues */
|
||||
$foo = ['1'=>1,'2'=>2,'3'=>3];
|
||||
|
||||
/* testMissingKeys */
|
||||
$foo = ['1'=>1,2,'3'=>3];
|
||||
|
||||
/* testMultiTokenKeys */
|
||||
$paths = array(
|
||||
Init::ROOT_DIR.'/a' => 'a',
|
||||
Init::ROOT_DIR.'/b' => 'b',
|
||||
);
|
||||
|
||||
/* testMissingKeysCoalesceTernary */
|
||||
return [
|
||||
$a => static function () { return [1,2,3]; },
|
||||
$b ?? $c,
|
||||
$d ? [$e] : [$f],
|
||||
];
|
||||
|
||||
/* testTernaryValues */
|
||||
$foo = [
|
||||
'1' => $row['status'] === 'rejected'
|
||||
? self::REJECTED_CODE
|
||||
: self::VERIFIED_CODE,
|
||||
'2' => in_array($row['status'], array('notverified', 'unverified'), true)
|
||||
? self::STATUS_PENDING
|
||||
: self::STATUS_VERIFIED,
|
||||
'3' => strtotime($row['date']),
|
||||
];
|
||||
|
||||
/* testHeredocValues */
|
||||
$foo = array(
|
||||
<<<HERE
|
||||
HERE
|
||||
,
|
||||
<<<HERE
|
||||
HERE
|
||||
,
|
||||
);
|
||||
|
||||
/* testArrowFunctionValue */
|
||||
$foo = array(
|
||||
1 => '1',
|
||||
2 => fn ($x) => yield 'a' => $x,
|
||||
3 => '3',
|
||||
);
|
||||
Vendored
-297
@@ -1,297 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Sniffs\AbstractArraySniff.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2020 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Sniffs;
|
||||
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
|
||||
/**
|
||||
* Tests for the \PHP_CodeSniffer\Sniffs\AbstractArraySniff.
|
||||
*
|
||||
* @covers \PHP_CodeSniffer\Sniffs\AbstractArraySniff
|
||||
*/
|
||||
final class AbstractArraySniffTest extends AbstractMethodUnitTest
|
||||
{
|
||||
|
||||
/**
|
||||
* The sniff objects we are testing.
|
||||
*
|
||||
* This extends the \PHP_CodeSniffer\Sniffs\AbstractArraySniff class to make the
|
||||
* internal workings of the sniff observable.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Sniffs\AbstractArraySniffTestable
|
||||
*/
|
||||
protected static $sniff;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
|
||||
*
|
||||
* The test case file for a unit test class has to be in the same directory
|
||||
* directory and use the same file name as the test class, using the .inc extension.
|
||||
*
|
||||
* @beforeClass
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function initializeFile()
|
||||
{
|
||||
self::$sniff = new AbstractArraySniffTestable();
|
||||
parent::initializeFile();
|
||||
|
||||
}//end initializeFile()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of simple values only.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSimpleValues()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testSimpleValues */', T_OPEN_SHORT_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => ['value_start' => ($token + 1)],
|
||||
1 => ['value_start' => ($token + 3)],
|
||||
2 => ['value_start' => ($token + 5)],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testSimpleValues()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of simple keys and values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSimpleKeyValues()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testSimpleKeyValues */', T_OPEN_SHORT_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 1),
|
||||
'index_end' => ($token + 1),
|
||||
'arrow' => ($token + 2),
|
||||
'value_start' => ($token + 3),
|
||||
],
|
||||
1 => [
|
||||
'index_start' => ($token + 5),
|
||||
'index_end' => ($token + 5),
|
||||
'arrow' => ($token + 6),
|
||||
'value_start' => ($token + 7),
|
||||
],
|
||||
2 => [
|
||||
'index_start' => ($token + 9),
|
||||
'index_end' => ($token + 9),
|
||||
'arrow' => ($token + 10),
|
||||
'value_start' => ($token + 11),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testSimpleKeyValues()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of simple keys and values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMissingKeys()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testMissingKeys */', T_OPEN_SHORT_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 1),
|
||||
'index_end' => ($token + 1),
|
||||
'arrow' => ($token + 2),
|
||||
'value_start' => ($token + 3),
|
||||
],
|
||||
1 => [
|
||||
'value_start' => ($token + 5),
|
||||
],
|
||||
2 => [
|
||||
'index_start' => ($token + 7),
|
||||
'index_end' => ($token + 7),
|
||||
'arrow' => ($token + 8),
|
||||
'value_start' => ($token + 9),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testMissingKeys()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array with keys that span multiple tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMultiTokenKeys()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testMultiTokenKeys */', T_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 4),
|
||||
'index_end' => ($token + 8),
|
||||
'arrow' => ($token + 10),
|
||||
'value_start' => ($token + 12),
|
||||
],
|
||||
1 => [
|
||||
'index_start' => ($token + 16),
|
||||
'index_end' => ($token + 20),
|
||||
'arrow' => ($token + 22),
|
||||
'value_start' => ($token + 24),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testMultiTokenKeys()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of simple keys and values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMissingKeysCoalesceTernary()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testMissingKeysCoalesceTernary */', T_OPEN_SHORT_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 3),
|
||||
'index_end' => ($token + 3),
|
||||
'arrow' => ($token + 5),
|
||||
'value_start' => ($token + 7),
|
||||
],
|
||||
1 => [
|
||||
'value_start' => ($token + 31),
|
||||
],
|
||||
2 => [
|
||||
'value_start' => ($token + 39),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testMissingKeysCoalesceTernary()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of ternary values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTernaryValues()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testTernaryValues */', T_OPEN_SHORT_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 3),
|
||||
'index_end' => ($token + 3),
|
||||
'arrow' => ($token + 5),
|
||||
'value_start' => ($token + 7),
|
||||
],
|
||||
1 => [
|
||||
'index_start' => ($token + 32),
|
||||
'index_end' => ($token + 32),
|
||||
'arrow' => ($token + 34),
|
||||
'value_start' => ($token + 36),
|
||||
],
|
||||
2 => [
|
||||
'index_start' => ($token + 72),
|
||||
'index_end' => ($token + 72),
|
||||
'arrow' => ($token + 74),
|
||||
'value_start' => ($token + 76),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testTernaryValues()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of heredocs.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testHeredocValues()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testHeredocValues */', T_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'value_start' => ($token + 4),
|
||||
],
|
||||
1 => [
|
||||
'value_start' => ($token + 10),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testHeredocValues()
|
||||
|
||||
|
||||
/**
|
||||
* Test an array of with an arrow function as a value.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrowFunctionValue()
|
||||
{
|
||||
$token = $this->getTargetToken('/* testArrowFunctionValue */', T_ARRAY);
|
||||
self::$sniff->process(self::$phpcsFile, $token);
|
||||
|
||||
$expected = [
|
||||
0 => [
|
||||
'index_start' => ($token + 4),
|
||||
'index_end' => ($token + 4),
|
||||
'arrow' => ($token + 6),
|
||||
'value_start' => ($token + 8),
|
||||
],
|
||||
1 => [
|
||||
'index_start' => ($token + 12),
|
||||
'index_end' => ($token + 12),
|
||||
'arrow' => ($token + 14),
|
||||
'value_start' => ($token + 16),
|
||||
],
|
||||
2 => [
|
||||
'index_start' => ($token + 34),
|
||||
'index_end' => ($token + 34),
|
||||
'arrow' => ($token + 36),
|
||||
'value_start' => ($token + 38),
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, self::$sniff->indicies);
|
||||
|
||||
}//end testArrowFunctionValue()
|
||||
|
||||
|
||||
}//end class
|
||||
-65
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* A testable implementation of \PHP_CodeSniffer\Sniffs\AbstractArraySniff.
|
||||
*
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @copyright 2006-2020 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Sniffs;
|
||||
|
||||
use PHP_CodeSniffer\Sniffs\AbstractArraySniff;
|
||||
|
||||
class AbstractArraySniffTestable extends AbstractArraySniff
|
||||
{
|
||||
|
||||
/**
|
||||
* The array indicies that were found during processing.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $indicies = [];
|
||||
|
||||
|
||||
/**
|
||||
* Processes a single-line array definition.
|
||||
*
|
||||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param int $arrayStart The token that starts the array definition.
|
||||
* @param int $arrayEnd The token that ends the array definition.
|
||||
* @param array $indices An array of token positions for the array keys,
|
||||
* double arrows, and values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices)
|
||||
{
|
||||
$this->indicies = $indices;
|
||||
|
||||
}//end processSingleLineArray()
|
||||
|
||||
|
||||
/**
|
||||
* Processes a multi-line array definition.
|
||||
*
|
||||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
|
||||
* @param int $stackPtr The position of the current token
|
||||
* in the stack passed in $tokens.
|
||||
* @param int $arrayStart The token that starts the array definition.
|
||||
* @param int $arrayEnd The token that ends the array definition.
|
||||
* @param array $indices An array of token positions for the array keys,
|
||||
* double arrows, and values.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd, $indices)
|
||||
{
|
||||
$this->indicies = $indices;
|
||||
|
||||
}//end processMultiLineArray()
|
||||
|
||||
|
||||
}//end class
|
||||
-124
@@ -1,124 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class to use when testing parts of the tokenizer.
|
||||
*
|
||||
* This is a near duplicate of the AbstractMethodUnitTest class, with the
|
||||
* difference being that it allows for recording code coverage for tokenizer tests.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2018-2019 Juliette Reinders Folmer. All rights reserved.
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
|
||||
|
||||
use PHP_CodeSniffer\Ruleset;
|
||||
use PHP_CodeSniffer\Files\DummyFile;
|
||||
use PHP_CodeSniffer\Tests\ConfigDouble;
|
||||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionProperty;
|
||||
|
||||
abstract class AbstractTokenizerTestCase extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The file extension of the test case file (without leading dot).
|
||||
*
|
||||
* This allows child classes to overrule the default `inc` with, for instance,
|
||||
* `js` or `css` when applicable.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fileExtension = 'inc';
|
||||
|
||||
/**
|
||||
* The tab width setting to use when tokenizing the file.
|
||||
*
|
||||
* This allows for test case files to use a different tab width than the default.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $tabWidth = 4;
|
||||
|
||||
/**
|
||||
* The \PHP_CodeSniffer\Files\File object containing the parsed contents of the test case file.
|
||||
*
|
||||
* @var \PHP_CodeSniffer\Files\File
|
||||
*/
|
||||
protected $phpcsFile;
|
||||
|
||||
|
||||
/**
|
||||
* Initialize & tokenize \PHP_CodeSniffer\Files\File with code from the test case file.
|
||||
*
|
||||
* The test case file for a unit test class has to be in the same directory
|
||||
* directory and use the same file name as the test class, using the .inc extension.
|
||||
*
|
||||
* @before
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function initializeFile()
|
||||
{
|
||||
if (isset($this->phpcsFile) === false) {
|
||||
$config = new ConfigDouble();
|
||||
// Also set a tab-width to enable testing tab-replaced vs `orig_content`.
|
||||
$config->tabWidth = $this->tabWidth;
|
||||
|
||||
$ruleset = new Ruleset($config);
|
||||
|
||||
// Default to a file with the same name as the test class. Extension is property based.
|
||||
$relativeCN = str_replace(__NAMESPACE__, '', get_called_class());
|
||||
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeCN);
|
||||
$pathToTestFile = realpath(__DIR__).$relativePath.'.'.$this->fileExtension;
|
||||
|
||||
// Make sure the file gets parsed correctly based on the file type.
|
||||
$contents = 'phpcs_input_file: '.$pathToTestFile.PHP_EOL;
|
||||
$contents .= file_get_contents($pathToTestFile);
|
||||
|
||||
$this->phpcsFile = new DummyFile($contents, $ruleset, $config);
|
||||
$this->phpcsFile->process();
|
||||
}
|
||||
|
||||
}//end initializeFile()
|
||||
|
||||
|
||||
/**
|
||||
* Get the token pointer for a target token based on a specific comment found on the line before.
|
||||
*
|
||||
* Note: the test delimiter comment MUST start with "/* test" to allow this function to
|
||||
* distinguish between comments used *in* a test and test delimiters.
|
||||
*
|
||||
* @param string $commentString The delimiter comment to look for.
|
||||
* @param int|string|array $tokenType The type of token(s) to look for.
|
||||
* @param string $tokenContent Optional. The token content for the target token.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getTargetToken($commentString, $tokenType, $tokenContent=null)
|
||||
{
|
||||
return AbstractMethodUnitTest::getTargetTokenFromFile($this->phpcsFile, $commentString, $tokenType, $tokenContent);
|
||||
|
||||
}//end getTargetToken()
|
||||
|
||||
|
||||
/**
|
||||
* Clear the static "resolved tokens" cache property on the Tokenizer\PHP class.
|
||||
*
|
||||
* This method should be used selectively by tests to ensure the code under test is actually hit
|
||||
* by the test testing the code.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clearResolvedTokensCache()
|
||||
{
|
||||
$property = new ReflectionProperty('PHP_CodeSniffer\Tokenizers\PHP', 'resolveTokenCache');
|
||||
$property->setAccessible(true);
|
||||
$property->setValue(null, []);
|
||||
$property->setAccessible(false);
|
||||
|
||||
}//end clearResolvedTokensCache()
|
||||
|
||||
|
||||
}//end class
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testNoParentheses */
|
||||
$anonClass = new class {
|
||||
function __construct() {}
|
||||
};
|
||||
|
||||
/* testReadonlyNoParentheses */
|
||||
$anonClass = new readonly class {
|
||||
function __construct() {}
|
||||
};
|
||||
|
||||
/* testNoParenthesesAndEmptyTokens */
|
||||
$anonClass = new class // phpcs:ignore Standard.Cat
|
||||
{
|
||||
function __construct() {}
|
||||
};
|
||||
|
||||
/* testWithParentheses */
|
||||
$anonClass = new class() {};
|
||||
|
||||
/* testReadonlyWithParentheses */
|
||||
$anonClass = new readonly class() {
|
||||
function __construct() {}
|
||||
};
|
||||
|
||||
/* testWithParenthesesAndEmptyTokens */
|
||||
$anonClass = new class /*comment */
|
||||
() {};
|
||||
-156
@@ -1,156 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests the adding of the "parenthesis" keys to an anonymous class token.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
|
||||
|
||||
final class AnonClassParenthesisOwnerTest extends AbstractTokenizerTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test that anonymous class tokens without parenthesis do not get assigned a parenthesis owner.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
*
|
||||
* @dataProvider dataAnonClassNoParentheses
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAnonClassNoParentheses($testMarker)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
|
||||
$this->assertFalse(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
|
||||
$this->assertFalse(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
|
||||
$this->assertFalse(array_key_exists('parenthesis_closer', $tokens[$anonClass]));
|
||||
|
||||
}//end testAnonClassNoParentheses()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the next open/close parenthesis after an anonymous class without parenthesis
|
||||
* do not get assigned the anonymous class as a parenthesis owner.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
*
|
||||
* @dataProvider dataAnonClassNoParentheses
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAnonClassNoParenthesesNextOpenClose($testMarker)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
$function = $this->getTargetToken($testMarker, T_FUNCTION);
|
||||
|
||||
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
|
||||
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
|
||||
$this->assertSame($function, $tokens[$opener]['parenthesis_owner']);
|
||||
|
||||
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
|
||||
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
|
||||
$this->assertSame($function, $tokens[$closer]['parenthesis_owner']);
|
||||
|
||||
}//end testAnonClassNoParenthesesNextOpenClose()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAnonClassNoParentheses()
|
||||
* @see testAnonClassNoParenthesesNextOpenClose()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataAnonClassNoParentheses()
|
||||
{
|
||||
return [
|
||||
'plain' => [
|
||||
'testMarker' => '/* testNoParentheses */',
|
||||
],
|
||||
'readonly' => [
|
||||
'testMarker' => '/* testReadonlyNoParentheses */',
|
||||
],
|
||||
'declaration contains comments and extra whitespace' => [
|
||||
'testMarker' => '/* testNoParenthesesAndEmptyTokens */',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataAnonClassNoParentheses()
|
||||
|
||||
|
||||
/**
|
||||
* Test that anonymous class tokens with parenthesis get assigned a parenthesis owner,
|
||||
* opener and closer; and that the opener/closer get the anonymous class assigned as owner.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
*
|
||||
* @dataProvider dataAnonClassWithParentheses
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAnonClassWithParentheses($testMarker)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
$anonClass = $this->getTargetToken($testMarker, T_ANON_CLASS);
|
||||
$opener = $this->getTargetToken($testMarker, T_OPEN_PARENTHESIS);
|
||||
$closer = $this->getTargetToken($testMarker, T_CLOSE_PARENTHESIS);
|
||||
|
||||
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$anonClass]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$anonClass]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$anonClass]));
|
||||
$this->assertSame($anonClass, $tokens[$anonClass]['parenthesis_owner']);
|
||||
$this->assertSame($opener, $tokens[$anonClass]['parenthesis_opener']);
|
||||
$this->assertSame($closer, $tokens[$anonClass]['parenthesis_closer']);
|
||||
|
||||
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$opener]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$opener]));
|
||||
$this->assertSame($anonClass, $tokens[$opener]['parenthesis_owner']);
|
||||
$this->assertSame($opener, $tokens[$opener]['parenthesis_opener']);
|
||||
$this->assertSame($closer, $tokens[$opener]['parenthesis_closer']);
|
||||
|
||||
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$closer]));
|
||||
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$closer]));
|
||||
$this->assertSame($anonClass, $tokens[$closer]['parenthesis_owner']);
|
||||
$this->assertSame($opener, $tokens[$closer]['parenthesis_opener']);
|
||||
$this->assertSame($closer, $tokens[$closer]['parenthesis_closer']);
|
||||
|
||||
}//end testAnonClassWithParentheses()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAnonClassWithParentheses()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataAnonClassWithParentheses()
|
||||
{
|
||||
return [
|
||||
'plain' => [
|
||||
'testMarker' => '/* testWithParentheses */',
|
||||
],
|
||||
'readonly' => [
|
||||
'testMarker' => '/* testReadonlyWithParentheses */',
|
||||
],
|
||||
'declaration contains comments and extra whitespace' => [
|
||||
'testMarker' => '/* testWithParenthesesAndEmptyTokens */',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataAnonClassWithParentheses()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-41
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testEmptyArray */
|
||||
$var = array();
|
||||
|
||||
/* testArrayWithSpace */
|
||||
$var = array (1 => 10);
|
||||
|
||||
/* testArrayWithComment */
|
||||
$var = Array /*comment*/ (1 => 10);
|
||||
|
||||
/* testNestingArray */
|
||||
$var = array(
|
||||
/* testNestedArray */
|
||||
array(
|
||||
'key' => 'value',
|
||||
|
||||
/* testClosureReturnType */
|
||||
'closure' => function($a) use($global) : Array {},
|
||||
),
|
||||
);
|
||||
|
||||
/* testFunctionDeclarationParamType */
|
||||
function typedParam(array $a) {}
|
||||
|
||||
/* testFunctionDeclarationReturnType */
|
||||
function returnType($a) : int|array|null {}
|
||||
|
||||
class Bar {
|
||||
/* testClassConst */
|
||||
const ARRAY = [];
|
||||
|
||||
/* testClassMethod */
|
||||
public function array() {}
|
||||
|
||||
/* testOOConstType */
|
||||
const array /* testTypedOOConstName */ ARRAY = /* testOOConstDefault */ array();
|
||||
|
||||
/* testOOPropertyType */
|
||||
protected array $property;
|
||||
}
|
||||
Vendored
-195
@@ -1,195 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests that the array keyword is tokenized correctly.
|
||||
*
|
||||
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
|
||||
* @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
|
||||
|
||||
final class ArrayKeywordTest extends AbstractTokenizerTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test that the array keyword is correctly tokenized as `T_ARRAY`.
|
||||
*
|
||||
* @param string $testMarker The comment prefacing the target token.
|
||||
* @param string $testContent Optional. The token content to look for.
|
||||
*
|
||||
* @dataProvider dataArrayKeyword
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayKeyword($testMarker, $testContent='array')
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
|
||||
$tokenArray = $tokens[$token];
|
||||
|
||||
$this->assertSame(T_ARRAY, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (code)');
|
||||
$this->assertSame('T_ARRAY', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (type)');
|
||||
|
||||
$this->assertArrayHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is not set');
|
||||
$this->assertArrayHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is not set');
|
||||
$this->assertArrayHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is not set');
|
||||
|
||||
}//end testArrayKeyword()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testArrayKeyword()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataArrayKeyword()
|
||||
{
|
||||
return [
|
||||
'empty array' => [
|
||||
'testMarker' => '/* testEmptyArray */',
|
||||
],
|
||||
'array with space before parenthesis' => [
|
||||
'testMarker' => '/* testArrayWithSpace */',
|
||||
],
|
||||
'array with comment before parenthesis' => [
|
||||
'testMarker' => '/* testArrayWithComment */',
|
||||
'testContent' => 'Array',
|
||||
],
|
||||
'nested: outer array' => [
|
||||
'testMarker' => '/* testNestingArray */',
|
||||
],
|
||||
'nested: inner array' => [
|
||||
'testMarker' => '/* testNestedArray */',
|
||||
],
|
||||
'OO constant default value' => [
|
||||
'testMarker' => '/* testOOConstDefault */',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataArrayKeyword()
|
||||
|
||||
|
||||
/**
|
||||
* Test that the array keyword when used in a type declaration is correctly tokenized as `T_STRING`.
|
||||
*
|
||||
* @param string $testMarker The comment prefacing the target token.
|
||||
* @param string $testContent Optional. The token content to look for.
|
||||
*
|
||||
* @dataProvider dataArrayType
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayType($testMarker, $testContent='array')
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
|
||||
$tokenArray = $tokens[$token];
|
||||
|
||||
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
|
||||
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)');
|
||||
|
||||
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
|
||||
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
|
||||
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
|
||||
|
||||
}//end testArrayType()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testArrayType()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataArrayType()
|
||||
{
|
||||
return [
|
||||
'closure return type' => [
|
||||
'testMarker' => '/* testClosureReturnType */',
|
||||
'testContent' => 'Array',
|
||||
],
|
||||
'function param type' => [
|
||||
'testMarker' => '/* testFunctionDeclarationParamType */',
|
||||
],
|
||||
'function union return type' => [
|
||||
'testMarker' => '/* testFunctionDeclarationReturnType */',
|
||||
],
|
||||
'OO constant type' => [
|
||||
'testMarker' => '/* testOOConstType */',
|
||||
],
|
||||
'OO property type' => [
|
||||
'testMarker' => '/* testOOPropertyType */',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataArrayType()
|
||||
|
||||
|
||||
/**
|
||||
* Verify that the retokenization of `T_ARRAY` tokens to `T_STRING` is handled correctly
|
||||
* for tokens with the contents 'array' which aren't in actual fact the array keyword.
|
||||
*
|
||||
* @param string $testMarker The comment prefacing the target token.
|
||||
* @param string $testContent The token content to look for.
|
||||
*
|
||||
* @dataProvider dataNotArrayKeyword
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNotArrayKeyword($testMarker, $testContent='array')
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent);
|
||||
$tokenArray = $tokens[$token];
|
||||
|
||||
$this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)');
|
||||
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)');
|
||||
|
||||
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
|
||||
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
|
||||
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
|
||||
|
||||
}//end testNotArrayKeyword()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testNotArrayKeyword()
|
||||
*
|
||||
* @return array<string, array<string, string>>
|
||||
*/
|
||||
public static function dataNotArrayKeyword()
|
||||
{
|
||||
return [
|
||||
'class-constant-name' => [
|
||||
'testMarker' => '/* testClassConst */',
|
||||
'testContent' => 'ARRAY',
|
||||
],
|
||||
'class-method-name' => [
|
||||
'testMarker' => '/* testClassMethod */',
|
||||
],
|
||||
'class-constant-name-after-type' => [
|
||||
'testMarker' => '/* testTypedOOConstName */',
|
||||
'testContent' => 'ARRAY',
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataNotArrayKeyword()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-90
@@ -1,90 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testAttribute */
|
||||
#[Attribute]
|
||||
class CustomAttribute {}
|
||||
|
||||
/* testAttributeWithParams */
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class SecondCustomAttribute {}
|
||||
|
||||
/* testAttributeWithNamedParam */
|
||||
#[Attribute(flags: Attribute::TARGET_ALL)]
|
||||
class AttributeWithParams {
|
||||
public function __construct($foo, array $bar) {}
|
||||
}
|
||||
|
||||
/* testAttributeOnFunction */
|
||||
#[CustomAttribute]
|
||||
function attribute_on_function_test() {}
|
||||
|
||||
/* testAttributeOnFunctionWithParams */
|
||||
#[AttributeWithParams('foo', bar: ['bar' => 'foobar'])]
|
||||
function attribute_with_params_on_function_test() {}
|
||||
|
||||
/* testAttributeWithShortClosureParameter */
|
||||
#[AttributeWithParams(static fn ($value) => ! $value)]
|
||||
function attribute_with_short_closure_param_test() {}
|
||||
|
||||
/* testTwoAttributeOnTheSameLine */
|
||||
#[CustomAttribute] #[AttributeWithParams('foo')]
|
||||
function two_attribute_on_same_line_test() {}
|
||||
|
||||
/* testAttributeAndCommentOnTheSameLine */
|
||||
#[CustomAttribute] // This is a comment
|
||||
function attribute_and_line_comment_on_same_line_test() {}
|
||||
|
||||
/* testAttributeGrouping */
|
||||
#[CustomAttribute, AttributeWithParams('foo'), AttributeWithParams('foo', bar: ['bar' => 'foobar'])]
|
||||
function attribute_grouping_test() {}
|
||||
|
||||
/* testAttributeMultiline */
|
||||
#[
|
||||
CustomAttribute,
|
||||
AttributeWithParams('foo'),
|
||||
AttributeWithParams('foo', bar: ['bar' => 'foobar'])
|
||||
]
|
||||
function attribute_multiline_test() {}
|
||||
|
||||
/* testAttributeMultilineWithComment */
|
||||
#[
|
||||
CustomAttribute, // comment
|
||||
AttributeWithParams(/* another comment */ 'foo'),
|
||||
AttributeWithParams('foo', bar: ['bar' => 'foobar'])
|
||||
]
|
||||
function attribute_multiline_with_comment_test() {}
|
||||
|
||||
/* testSingleAttributeOnParameter */
|
||||
function single_attribute_on_parameter_test(#[ParamAttribute] int $param) {}
|
||||
|
||||
/* testMultipleAttributesOnParameter */
|
||||
function multiple_attributes_on_parameter_test(#[ParamAttribute, AttributeWithParams(/* another comment */ 'foo')] int $param) {}
|
||||
|
||||
/* testFqcnAttribute */
|
||||
#[Boo\QualifiedName, \Foo\FullyQualifiedName('foo')]
|
||||
function fqcn_attrebute_test() {}
|
||||
|
||||
/* testNestedAttributes */
|
||||
#[Boo\QualifiedName(fn (#[AttributeOne('boo')] $value) => (string) $value)]
|
||||
function nested_attributes_test() {}
|
||||
|
||||
/* testMultilineAttributesOnParameter */
|
||||
function multiline_attributes_on_parameter_test(#[
|
||||
AttributeWithParams(
|
||||
'foo'
|
||||
)
|
||||
] int $param) {}
|
||||
|
||||
/* testAttributeContainingTextLookingLikeCloseTag */
|
||||
#[DeprecationReason('reason: <https://some-website/reason?>')]
|
||||
function attribute_containing_text_looking_like_close_tag() {}
|
||||
|
||||
/* testAttributeContainingMultilineTextLookingLikeCloseTag */
|
||||
#[DeprecationReason(
|
||||
'reason: <https://some-website/reason?>'
|
||||
)]
|
||||
function attribute_containing_mulitline_text_looking_like_close_tag() {}
|
||||
|
||||
/* testInvalidAttribute */
|
||||
#[ThisIsNotAnAttribute
|
||||
function invalid_attribute_test() {}
|
||||
Vendored
-702
@@ -1,702 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests the support of PHP 8 attributes
|
||||
*
|
||||
* @author Alessandro Chitolina <alekitto@gmail.com>
|
||||
* @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
||||
*/
|
||||
|
||||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
|
||||
|
||||
final class AttributesTest extends AbstractTokenizerTestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test that attributes are parsed correctly.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int $length The number of tokens between opener and closer.
|
||||
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
|
||||
*
|
||||
* @dataProvider dataAttribute
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAttribute($testMarker, $length, $tokenCodes)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$attribute = $this->getTargetToken($testMarker, T_ATTRIBUTE);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(($attribute + $length), $closer);
|
||||
|
||||
$this->assertSame(T_ATTRIBUTE_END, $tokens[$closer]['code']);
|
||||
|
||||
$this->assertSame($tokens[$attribute]['attribute_opener'], $tokens[$closer]['attribute_opener']);
|
||||
$this->assertSame($tokens[$attribute]['attribute_closer'], $tokens[$closer]['attribute_closer']);
|
||||
|
||||
$map = array_map(
|
||||
function ($token) use ($attribute, $length) {
|
||||
$this->assertArrayHasKey('attribute_closer', $token);
|
||||
$this->assertSame(($attribute + $length), $token['attribute_closer']);
|
||||
|
||||
return $token['code'];
|
||||
},
|
||||
array_slice($tokens, ($attribute + 1), ($length - 1))
|
||||
);
|
||||
|
||||
$this->assertSame($tokenCodes, $map);
|
||||
|
||||
}//end testAttribute()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAttribute()
|
||||
*
|
||||
* @return array<string, array<string, string|int|array<int|string>>>
|
||||
*/
|
||||
public static function dataAttribute()
|
||||
{
|
||||
return [
|
||||
'class attribute' => [
|
||||
'testMarker' => '/* testAttribute */',
|
||||
'length' => 2,
|
||||
'tokenCodes' => [
|
||||
T_STRING
|
||||
],
|
||||
],
|
||||
'class attribute with param' => [
|
||||
'testMarker' => '/* testAttributeWithParams */',
|
||||
'length' => 7,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_STRING,
|
||||
T_DOUBLE_COLON,
|
||||
T_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'class attribute with named param' => [
|
||||
'testMarker' => '/* testAttributeWithNamedParam */',
|
||||
'length' => 10,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_PARAM_NAME,
|
||||
T_COLON,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_DOUBLE_COLON,
|
||||
T_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'function attribute' => [
|
||||
'testMarker' => '/* testAttributeOnFunction */',
|
||||
'length' => 2,
|
||||
'tokenCodes' => [
|
||||
T_STRING
|
||||
],
|
||||
],
|
||||
'function attribute with params' => [
|
||||
'testMarker' => '/* testAttributeOnFunctionWithParams */',
|
||||
'length' => 17,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_PARAM_NAME,
|
||||
T_COLON,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_SHORT_ARRAY,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_WHITESPACE,
|
||||
T_DOUBLE_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_SHORT_ARRAY,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'function attribute with arrow function as param' => [
|
||||
'testMarker' => '/* testAttributeWithShortClosureParameter */',
|
||||
'length' => 17,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_STATIC,
|
||||
T_WHITESPACE,
|
||||
T_FN,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_VARIABLE,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_FN_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_BOOLEAN_NOT,
|
||||
T_WHITESPACE,
|
||||
T_VARIABLE,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'function attribute; multiple comma separated classes' => [
|
||||
'testMarker' => '/* testAttributeGrouping */',
|
||||
'length' => 26,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_PARAM_NAME,
|
||||
T_COLON,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_SHORT_ARRAY,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_WHITESPACE,
|
||||
T_DOUBLE_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_SHORT_ARRAY,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'function attribute; multiple comma separated classes, one per line' => [
|
||||
'testMarker' => '/* testAttributeMultiline */',
|
||||
'length' => 31,
|
||||
'tokenCodes' => [
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_PARAM_NAME,
|
||||
T_COLON,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_SHORT_ARRAY,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_WHITESPACE,
|
||||
T_DOUBLE_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_SHORT_ARRAY,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
],
|
||||
],
|
||||
'function attribute; multiple comma separated classes, one per line, with comments' => [
|
||||
'testMarker' => '/* testAttributeMultilineWithComment */',
|
||||
'length' => 34,
|
||||
'tokenCodes' => [
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_COMMENT,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_COMMENT,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_PARAM_NAME,
|
||||
T_COLON,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_SHORT_ARRAY,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_WHITESPACE,
|
||||
T_DOUBLE_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_SHORT_ARRAY,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
],
|
||||
],
|
||||
'function attribute; using partially qualified and fully qualified class names' => [
|
||||
'testMarker' => '/* testFqcnAttribute */',
|
||||
'length' => 13,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_NS_SEPARATOR,
|
||||
T_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_NS_SEPARATOR,
|
||||
T_STRING,
|
||||
T_NS_SEPARATOR,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataAttribute()
|
||||
|
||||
|
||||
/**
|
||||
* Test that multiple attributes on the same line are parsed correctly.
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTwoAttributesOnTheSameLine()
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$attribute = $this->getTargetToken('/* testTwoAttributeOnTheSameLine */', T_ATTRIBUTE);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(T_WHITESPACE, $tokens[($closer + 1)]['code']);
|
||||
$this->assertSame(T_ATTRIBUTE, $tokens[($closer + 2)]['code']);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[($closer + 2)]);
|
||||
|
||||
}//end testTwoAttributesOnTheSameLine()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attribute followed by a line comment is parsed correctly.
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAttributeAndLineComment()
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$attribute = $this->getTargetToken('/* testAttributeAndCommentOnTheSameLine */', T_ATTRIBUTE);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(T_WHITESPACE, $tokens[($closer + 1)]['code']);
|
||||
$this->assertSame(T_COMMENT, $tokens[($closer + 2)]['code']);
|
||||
|
||||
}//end testAttributeAndLineComment()
|
||||
|
||||
|
||||
/**
|
||||
* Test that attributes on function declaration parameters are parsed correctly.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int $position The token position (starting from T_FUNCTION) of T_ATTRIBUTE token.
|
||||
* @param int $length The number of tokens between opener and closer.
|
||||
* @param array<int|string> $tokenCodes The codes of tokens inside the attributes.
|
||||
*
|
||||
* @dataProvider dataAttributeOnParameters
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAttributeOnParameters($testMarker, $position, $length, array $tokenCodes)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$function = $this->getTargetToken($testMarker, T_FUNCTION);
|
||||
$attribute = ($function + $position);
|
||||
|
||||
$this->assertSame(T_ATTRIBUTE, $tokens[$attribute]['code']);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$this->assertSame(($attribute + $length), $tokens[$attribute]['attribute_closer']);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(T_WHITESPACE, $tokens[($closer + 1)]['code']);
|
||||
$this->assertSame(T_STRING, $tokens[($closer + 2)]['code']);
|
||||
$this->assertSame('int', $tokens[($closer + 2)]['content']);
|
||||
|
||||
$this->assertSame(T_VARIABLE, $tokens[($closer + 4)]['code']);
|
||||
$this->assertSame('$param', $tokens[($closer + 4)]['content']);
|
||||
|
||||
$map = array_map(
|
||||
function ($token) use ($attribute, $length) {
|
||||
$this->assertArrayHasKey('attribute_closer', $token);
|
||||
$this->assertSame(($attribute + $length), $token['attribute_closer']);
|
||||
|
||||
return $token['code'];
|
||||
},
|
||||
array_slice($tokens, ($attribute + 1), ($length - 1))
|
||||
);
|
||||
|
||||
$this->assertSame($tokenCodes, $map);
|
||||
|
||||
}//end testAttributeOnParameters()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see testAttributeOnParameters()
|
||||
*
|
||||
* @return array<string, array<string, string|int|array<int|string>>>
|
||||
*/
|
||||
public static function dataAttributeOnParameters()
|
||||
{
|
||||
return [
|
||||
'parameter attribute; single, inline' => [
|
||||
'testMarker' => '/* testSingleAttributeOnParameter */',
|
||||
'position' => 4,
|
||||
'length' => 2,
|
||||
'tokenCodes' => [
|
||||
T_STRING
|
||||
],
|
||||
],
|
||||
'parameter attribute; multiple comma separated, inline' => [
|
||||
'testMarker' => '/* testMultipleAttributesOnParameter */',
|
||||
'position' => 4,
|
||||
'length' => 10,
|
||||
'tokenCodes' => [
|
||||
T_STRING,
|
||||
T_COMMA,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_COMMENT,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
],
|
||||
],
|
||||
'parameter attribute; single, multiline' => [
|
||||
'testMarker' => '/* testMultilineAttributesOnParameter */',
|
||||
'position' => 4,
|
||||
'length' => 13,
|
||||
'tokenCodes' => [
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_WHITESPACE,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataAttributeOnParameters()
|
||||
|
||||
|
||||
/**
|
||||
* Test that an attribute containing text which looks like a PHP close tag is tokenized correctly.
|
||||
*
|
||||
* @param string $testMarker The comment which prefaces the target token in the test file.
|
||||
* @param int $length The number of tokens between opener and closer.
|
||||
* @param array<array<string>> $expectedTokensAttribute The codes of tokens inside the attributes.
|
||||
* @param array<int|string> $expectedTokensAfter The codes of tokens after the attributes.
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @dataProvider dataAttributeOnTextLookingLikeCloseTag
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAttributeContainingTextLookingLikeCloseTag($testMarker, $length, array $expectedTokensAttribute, array $expectedTokensAfter)
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$attribute = $this->getTargetToken($testMarker, T_ATTRIBUTE);
|
||||
|
||||
$this->assertSame('T_ATTRIBUTE', $tokens[$attribute]['type']);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(($attribute + $length), $closer);
|
||||
$this->assertSame(T_ATTRIBUTE_END, $tokens[$closer]['code']);
|
||||
$this->assertSame('T_ATTRIBUTE_END', $tokens[$closer]['type']);
|
||||
|
||||
$this->assertSame($tokens[$attribute]['attribute_opener'], $tokens[$closer]['attribute_opener']);
|
||||
$this->assertSame($tokens[$attribute]['attribute_closer'], $tokens[$closer]['attribute_closer']);
|
||||
|
||||
$i = ($attribute + 1);
|
||||
foreach ($expectedTokensAttribute as $item) {
|
||||
list($expectedType, $expectedContents) = $item;
|
||||
$this->assertSame($expectedType, $tokens[$i]['type']);
|
||||
$this->assertSame($expectedContents, $tokens[$i]['content']);
|
||||
$this->assertArrayHasKey('attribute_opener', $tokens[$i]);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$i]);
|
||||
++$i;
|
||||
}
|
||||
|
||||
$i = ($closer + 1);
|
||||
foreach ($expectedTokensAfter as $expectedCode) {
|
||||
$this->assertSame($expectedCode, $tokens[$i]['code']);
|
||||
++$i;
|
||||
}
|
||||
|
||||
}//end testAttributeContainingTextLookingLikeCloseTag()
|
||||
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @see dataAttributeOnTextLookingLikeCloseTag()
|
||||
*
|
||||
* @return array<string, array<string, string|int|array<array<string>>|array<int|string>>>
|
||||
*/
|
||||
public static function dataAttributeOnTextLookingLikeCloseTag()
|
||||
{
|
||||
return [
|
||||
'function attribute; string param with "?>"' => [
|
||||
'testMarker' => '/* testAttributeContainingTextLookingLikeCloseTag */',
|
||||
'length' => 5,
|
||||
'expectedTokensAttribute' => [
|
||||
[
|
||||
'T_STRING',
|
||||
'DeprecationReason',
|
||||
],
|
||||
[
|
||||
'T_OPEN_PARENTHESIS',
|
||||
'(',
|
||||
],
|
||||
[
|
||||
'T_CONSTANT_ENCAPSED_STRING',
|
||||
"'reason: <https://some-website/reason?>'",
|
||||
],
|
||||
[
|
||||
'T_CLOSE_PARENTHESIS',
|
||||
')',
|
||||
],
|
||||
[
|
||||
'T_ATTRIBUTE_END',
|
||||
']',
|
||||
],
|
||||
],
|
||||
'expectedTokensAfter' => [
|
||||
T_WHITESPACE,
|
||||
T_FUNCTION,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_CURLY_BRACKET,
|
||||
T_CLOSE_CURLY_BRACKET,
|
||||
],
|
||||
],
|
||||
'function attribute; string param with "?>"; multiline' => [
|
||||
'testMarker' => '/* testAttributeContainingMultilineTextLookingLikeCloseTag */',
|
||||
'length' => 8,
|
||||
'expectedTokensAttribute' => [
|
||||
[
|
||||
'T_STRING',
|
||||
'DeprecationReason',
|
||||
],
|
||||
[
|
||||
'T_OPEN_PARENTHESIS',
|
||||
'(',
|
||||
],
|
||||
[
|
||||
'T_WHITESPACE',
|
||||
"\n",
|
||||
],
|
||||
[
|
||||
'T_WHITESPACE',
|
||||
" ",
|
||||
],
|
||||
[
|
||||
'T_CONSTANT_ENCAPSED_STRING',
|
||||
"'reason: <https://some-website/reason?>'",
|
||||
],
|
||||
[
|
||||
'T_WHITESPACE',
|
||||
"\n",
|
||||
],
|
||||
[
|
||||
'T_CLOSE_PARENTHESIS',
|
||||
')',
|
||||
],
|
||||
[
|
||||
'T_ATTRIBUTE_END',
|
||||
']',
|
||||
],
|
||||
],
|
||||
'expectedTokensAfter' => [
|
||||
T_WHITESPACE,
|
||||
T_FUNCTION,
|
||||
T_WHITESPACE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_CURLY_BRACKET,
|
||||
T_CLOSE_CURLY_BRACKET,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
}//end dataAttributeOnTextLookingLikeCloseTag()
|
||||
|
||||
|
||||
/**
|
||||
* Test that invalid attribute (or comment starting with #[ and without ]) are parsed correctly.
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidAttribute()
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
|
||||
$attribute = $this->getTargetToken('/* testInvalidAttribute */', T_ATTRIBUTE);
|
||||
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
$this->assertNull($tokens[$attribute]['attribute_closer']);
|
||||
|
||||
}//end testInvalidAttribute()
|
||||
|
||||
|
||||
/**
|
||||
* Test that nested attributes are parsed correctly.
|
||||
*
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::findCloser
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::parsePhpAttribute
|
||||
* @covers PHP_CodeSniffer\Tokenizers\PHP::createAttributesNestingMap
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNestedAttributes()
|
||||
{
|
||||
$tokens = $this->phpcsFile->getTokens();
|
||||
$tokenCodes = [
|
||||
T_STRING,
|
||||
T_NS_SEPARATOR,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_FN,
|
||||
T_WHITESPACE,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_ATTRIBUTE,
|
||||
T_STRING,
|
||||
T_OPEN_PARENTHESIS,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_ATTRIBUTE_END,
|
||||
T_WHITESPACE,
|
||||
T_VARIABLE,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
T_WHITESPACE,
|
||||
T_FN_ARROW,
|
||||
T_WHITESPACE,
|
||||
T_STRING_CAST,
|
||||
T_WHITESPACE,
|
||||
T_VARIABLE,
|
||||
T_CLOSE_PARENTHESIS,
|
||||
];
|
||||
|
||||
$attribute = $this->getTargetToken('/* testNestedAttributes */', T_ATTRIBUTE);
|
||||
$this->assertArrayHasKey('attribute_closer', $tokens[$attribute]);
|
||||
|
||||
$closer = $tokens[$attribute]['attribute_closer'];
|
||||
$this->assertSame(($attribute + 24), $closer);
|
||||
|
||||
$this->assertSame(T_ATTRIBUTE_END, $tokens[$closer]['code']);
|
||||
|
||||
$this->assertSame($tokens[$attribute]['attribute_opener'], $tokens[$closer]['attribute_opener']);
|
||||
$this->assertSame($tokens[$attribute]['attribute_closer'], $tokens[$closer]['attribute_closer']);
|
||||
|
||||
$this->assertArrayNotHasKey('nested_attributes', $tokens[$attribute]);
|
||||
$this->assertArrayHasKey('nested_attributes', $tokens[($attribute + 8)]);
|
||||
$this->assertSame([$attribute => ($attribute + 24)], $tokens[($attribute + 8)]['nested_attributes']);
|
||||
|
||||
$test = function (array $tokens, $length, $nestedMap) use ($attribute) {
|
||||
foreach ($tokens as $token) {
|
||||
$this->assertArrayHasKey('attribute_closer', $token);
|
||||
$this->assertSame(($attribute + $length), $token['attribute_closer']);
|
||||
$this->assertSame($nestedMap, $token['nested_attributes']);
|
||||
}
|
||||
};
|
||||
|
||||
$test(array_slice($tokens, ($attribute + 1), 7), 24, [$attribute => $attribute + 24]);
|
||||
$test(array_slice($tokens, ($attribute + 8), 1), 8 + 5, [$attribute => $attribute + 24]);
|
||||
|
||||
// Length here is 8 (nested attribute offset) + 5 (real length).
|
||||
$test(
|
||||
array_slice($tokens, ($attribute + 9), 4),
|
||||
8 + 5,
|
||||
[
|
||||
$attribute => $attribute + 24,
|
||||
$attribute + 8 => $attribute + 13,
|
||||
]
|
||||
);
|
||||
|
||||
$test(array_slice($tokens, ($attribute + 13), 1), 8 + 5, [$attribute => $attribute + 24]);
|
||||
$test(array_slice($tokens, ($attribute + 14), 10), 24, [$attribute => $attribute + 24]);
|
||||
|
||||
$map = array_map(
|
||||
static function ($token) {
|
||||
return $token['code'];
|
||||
},
|
||||
array_slice($tokens, ($attribute + 1), 23)
|
||||
);
|
||||
|
||||
$this->assertSame($tokenCodes, $map);
|
||||
|
||||
}//end testNestedAttributes()
|
||||
|
||||
|
||||
}//end class
|
||||
Vendored
-91
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* testPureEnum */
|
||||
enum Foo
|
||||
{
|
||||
case SOME_CASE;
|
||||
}
|
||||
|
||||
/* testBackedIntEnum */
|
||||
enum Boo: int {
|
||||
case ONE = 1;
|
||||
case TWO = 1;
|
||||
}
|
||||
|
||||
/* testBackedStringEnum */
|
||||
enum Hoo : string
|
||||
{
|
||||
case ONE = 'one';
|
||||
case TWO = 'two';
|
||||
}
|
||||
|
||||
/* testComplexEnum */
|
||||
enum ComplexEnum: int implements SomeInterface
|
||||
{
|
||||
use SomeTrait {
|
||||
traitMethod as enumMethod;
|
||||
}
|
||||
|
||||
const SOME_CONSTANT = true;
|
||||
|
||||
case ONE = 1;
|
||||
|
||||
public function someMethod(): bool
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/* testEnumWithEnumAsClassName */
|
||||
enum /* testEnumAsClassNameAfterEnumKeyword */ Enum {}
|
||||
|
||||
/* testEnumIsCaseInsensitive */
|
||||
EnUm Enum {}
|
||||
|
||||
/* testEnumUsedAsClassName */
|
||||
class Enum {
|
||||
/* testEnumUsedAsClassConstantName */
|
||||
const ENUM = 'enum';
|
||||
|
||||
/* testEnumUsedAsMethodName */
|
||||
public function enum() {
|
||||
// Do something.
|
||||
|
||||
/* testEnumUsedAsPropertyName */
|
||||
$this->enum = 'foo';
|
||||
}
|
||||
}
|
||||
|
||||
/* testEnumUsedAsFunctionName */
|
||||
function enum()
|
||||
{
|
||||
}
|
||||
|
||||
/* testDeclarationContainingComment */
|
||||
enum /* comment */ Name
|
||||
{
|
||||
case SOME_CASE;
|
||||
}
|
||||
|
||||
/* testEnumUsedAsNamespaceName */
|
||||
namespace Enum;
|
||||
/* testEnumUsedAsPartOfNamespaceName */
|
||||
namespace My\Enum\Collection;
|
||||
/* testEnumUsedInObjectInitialization */
|
||||
$obj = new Enum;
|
||||
/* testEnumAsFunctionCall */
|
||||
$var = enum($a, $b);
|
||||
/* testEnumAsFunctionCallWithNamespace */
|
||||
var = namespace\enum();
|
||||
/* testClassConstantFetchWithEnumAsClassName */
|
||||
echo Enum::CONSTANT;
|
||||
/* testClassConstantFetchWithEnumAsConstantName */
|
||||
echo ClassName::ENUM;
|
||||
|
||||
/* testParseErrorMissingName */
|
||||
enum {
|
||||
case SOME_CASE;
|
||||
}
|
||||
|
||||
/* testParseErrorLiveCoding */
|
||||
// This must be the last test in the file.
|
||||
enum
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user