Skip to content

Commit c4692ca

Browse files
committed
Tokens\Collections: add two new methods
* `functionDeclarationTokens()` returning the tokens which can represent a keyword which starts a function declaration. * `functionDeclarationTokensBC()` same, but allowing for BC down to PHPCS 2.6.0 (arrow functions). Includes unit tests.
1 parent 0fa0724 commit c4692ca

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

PHPCSUtils/Tokens/Collections.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,72 @@ public static function arrowFunctionTokensBC()
531531
return $tokens;
532532
}
533533

534+
/**
535+
* Tokens which can represent a keyword which starts a function declaration.
536+
*
537+
* Note: this is a method, not a property as the `T_FN` token may not exist.
538+
*
539+
* Sister-method to the `functionDeclarationTokensBC()` method.
540+
* This method supports PHPCS 3.5.3 and up.
541+
* The `functionDeclarationTokensBC()` method supports PHPCS 2.6.0 and up.
542+
*
543+
* @see \PHPCSUtils\Tokens\Collections::functionDeclarationTokensBC() Related method (PHPCS 2.6.0+).
544+
*
545+
* @since 1.0.0
546+
*
547+
* @return array <int|string> => <int|string>
548+
*/
549+
public static function functionDeclarationTokens()
550+
{
551+
$tokens = [
552+
\T_FUNCTION => \T_FUNCTION,
553+
\T_CLOSURE => \T_CLOSURE,
554+
];
555+
556+
if (\defined('T_FN') === true) {
557+
// PHP 7.4 or PHPCS 3.5.3+.
558+
$tokens[\T_FN] = \T_FN;
559+
}
560+
561+
return $tokens;
562+
}
563+
564+
/**
565+
* Tokens which can represent a keyword which starts a function declaration.
566+
*
567+
* Note: this is a method, not a property as the `T_FN` token may not exist.
568+
*
569+
* Sister-method to the `functionDeclarationTokens()` method.
570+
* The `functionDeclarationTokens()` method supports PHPCS 3.5.3 and up.
571+
* This method supports PHPCS 2.6.0 and up.
572+
*
573+
* Notable difference:
574+
* This method accounts for when the `T_FN` token doesn't exist.
575+
* Note: if this method is used, the `FunctionDeclarations::isArrowFunction() method
576+
* needs to be used on arrow function tokens to verify whether it really is an arrow function
577+
* declaration or not.
578+
*
579+
* It is recommended to use the method instead of the property if a standard supports PHPCS < 3.3.0.
580+
*
581+
* @see \PHPCSUtils\Tokens\Collections::functionDeclarationTokens() Related method (PHPCS 3.5.3+).
582+
* @see \PHPCSUtils\Tokens\FunctionDeclarations::isArrowFunction() Arrow function verification.
583+
*
584+
* @since 1.0.0
585+
*
586+
* @return array <int|string> => <int|string>
587+
*/
588+
public static function functionDeclarationTokensBC()
589+
{
590+
$tokens = [
591+
\T_FUNCTION => \T_FUNCTION,
592+
\T_CLOSURE => \T_CLOSURE,
593+
];
594+
595+
$tokens += self::arrowFunctionTokensBC();
596+
597+
return $tokens;
598+
}
599+
534600
/**
535601
* Token types which can be encountered in a parameter type declaration (cross-version).
536602
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSUtils
6+
* @copyright 2019-2020 PHPCSUtils Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSUtils
9+
*/
10+
11+
namespace PHPCSUtils\Tests\Tokens\Collections;
12+
13+
use PHPCSUtils\BackCompat\Helper;
14+
use PHPCSUtils\Tokens\Collections;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test class.
19+
*
20+
* @covers \PHPCSUtils\Tokens\Collections::functionDeclarationTokensBC
21+
*
22+
* @group collections
23+
*
24+
* @since 1.0.0
25+
*/
26+
class FunctionDeclarationTokensBCTest extends TestCase
27+
{
28+
29+
/**
30+
* Test the method.
31+
*
32+
* @return void
33+
*/
34+
public function testFunctionDeclarationTokensBC()
35+
{
36+
$version = Helper::getVersion();
37+
$expected = [
38+
\T_FUNCTION => \T_FUNCTION,
39+
\T_CLOSURE => \T_CLOSURE,
40+
\T_STRING => \T_STRING,
41+
];
42+
43+
if (\version_compare($version, '3.5.3', '>=') === true
44+
|| \version_compare(\PHP_VERSION_ID, '70399', '>=') === true
45+
) {
46+
$expected[\T_FN] = \T_FN;
47+
}
48+
49+
$this->assertSame($expected, Collections::functionDeclarationTokensBC());
50+
}
51+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSUtils
6+
* @copyright 2019-2020 PHPCSUtils Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSUtils
9+
*/
10+
11+
namespace PHPCSUtils\Tests\Tokens\Collections;
12+
13+
use PHPCSUtils\BackCompat\Helper;
14+
use PHPCSUtils\Tokens\Collections;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test class.
19+
*
20+
* @covers \PHPCSUtils\Tokens\Collections::functionDeclarationTokens
21+
*
22+
* @group collections
23+
*
24+
* @since 1.0.0
25+
*/
26+
class FunctionDeclarationTokensTest extends TestCase
27+
{
28+
29+
/**
30+
* Test the method.
31+
*
32+
* @return void
33+
*/
34+
public function testFunctionDeclarationTokens()
35+
{
36+
$version = Helper::getVersion();
37+
$expected = [
38+
\T_FUNCTION => \T_FUNCTION,
39+
\T_CLOSURE => \T_CLOSURE,
40+
];
41+
42+
if (\version_compare($version, '3.5.3', '>=') === true
43+
|| \version_compare(\PHP_VERSION_ID, '70399', '>=') === true
44+
) {
45+
$expected[\T_FN] = \T_FN;
46+
}
47+
48+
$this->assertSame($expected, Collections::functionDeclarationTokens());
49+
}
50+
}

0 commit comments

Comments
 (0)