Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
[CS] Add allow_classes option to ClassStringToClassConstantFixer [closes
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 31, 2018
1 parent 30127fb commit 6956252
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
36 changes: 23 additions & 13 deletions packages/CodingStandard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Just like `PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer`, but this one removes all
This checker keeps 'mixed' and 'object' and other types by default. But if you need, you can **configure it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Commenting\RemoveUselessDocBlockFixer:
useless_types: ['mixed', 'object'] # [] by default
Expand Down Expand Up @@ -118,7 +118,7 @@ services:
This checker imports single name classes like `\Twig_Extension` or `\SplFileInfo` by default. But if you need, you can **configure it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Import\ImportNamespacedNameFixer:
allow_single_names: true # false by default
Expand All @@ -127,7 +127,7 @@ services:
You can also configure to check `/** @var Namespaced\DocBlocks */` as well:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Import\ImportNamespacedNameFixer:
include_doc_blocks: true # false by default
Expand Down Expand Up @@ -184,7 +184,7 @@ class SomeClass
**Change it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer:
max_line_length: 100 # default: 120
Expand All @@ -209,7 +209,7 @@ services:
This checker ignores few **system classes like `std*` or `Spl*` by default**. In case want to skip more classes, you can **configure it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
extra_skipped_classes:
Expand All @@ -229,12 +229,22 @@ services:
This checker takes **only existing classes by default**. In case want to check another code not loaded by local composer, you can **configure it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
class_must_exist: false # true by default
```

Do you want to allow some classes to be in string format?

```yaml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
allow_classes:
- 'SomeClass'
```

### Array property should have default value, to prevent undefined array issues

- class: [`Symplify\CodingStandard\Fixer\Property\ArrayPropertyDefaultValueFixer`](src/Fixer/Property/ArrayPropertyDefaultValueFixer.php)
Expand Down Expand Up @@ -284,7 +294,7 @@ services:
In case want check this only for specific interfaces, you can **configure them**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\Solid\FinalInterfaceFixer:
only_interfaces:
Expand Down Expand Up @@ -332,7 +342,7 @@ throw new FileNotFoundException('...');
- class: [`Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenParentClassSniff`](src/Sniffs/CleanCode/ForbiddenParentClassSniff.php)

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenParentClassSniff:
forbiddenParentClasses:
Expand Down Expand Up @@ -479,7 +489,7 @@ return sprintf('Class "%s" was removed from "%s". Use "%s" instead', $oldClass,
Is 2 `.` too strict? Just configure it:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Sniffs\ControlStructure\SprintfOverContactSniff:
maxConcatCount: 4 # "3" by default
Expand Down Expand Up @@ -552,7 +562,7 @@ This checkers ignores by default some classes, see `$allowedClasses` property.
In case want to exclude more classes, you can **configure it** with class or pattern using [`fnmatch`](http://php.net/manual/en/function.fnmatch.php):

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\DependencyInjection\NoClassInstantiationSniff:
extraAllowedClasses:
Expand All @@ -562,7 +572,7 @@ services:
Doctrine entities are skipped as well. You can disable that by:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Fixer\DependencyInjection\NoClassInstantiationSniff:
includeEntities: true
Expand Down Expand Up @@ -620,7 +630,7 @@ class SomeCommand extends Command
This checker check few names by default. But if you need, you can **configure it**:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Sniffs\Naming\ClassNameSuffixByParentSniff:
parentTypesToSuffixes:
Expand All @@ -641,7 +651,7 @@ services:
Or keep all defaults values by using `extra_parent_types_to_suffixes`:

```yaml
# easy-coding-standard.yml
# ecs.yml
services:
Symplify\CodingStandard\Sniffs\Naming\ClassNameSuffixByParentSniff:
extraParentTypesToSuffixes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ final class ClassStringToClassConstantFixer implements DefinedFixerInterface, Co
*/
public const CLASS_MUST_EXIST_OPTION = 'class_must_exist';

/**
* @var string
*/
public const ALLOW_CLASES_OPTION = 'allow_classes';

/**
* @var string
*/
Expand Down Expand Up @@ -125,7 +130,16 @@ public function getConfigurationDefinition(): FixerConfigurationResolverInterfac
->setDefault(true)
->getOption();

return new FixerConfigurationResolver([$classMustExistOption]);
$fixerOptionBuilder = new FixerOptionBuilder(
self::ALLOW_CLASES_OPTION,
'Classes allowed to be in string format.'
);

$allowedClassesOption = $fixerOptionBuilder->setAllowedTypes(['string[]'])
->setDefault([])
->getOption();

return new FixerConfigurationResolver([$classMustExistOption, $allowedClassesOption]);
}

private function getNameFromToken(Token $token): string
Expand All @@ -148,6 +162,12 @@ private function isClassInterfaceOrTrait(string $potentialClassInterfaceOrTrait)
return false;
}

foreach ($this->configuration[self::ALLOW_CLASES_OPTION] as $allowedClass) {
if ($potentialClassInterfaceOrTrait === $allowedClass) {
return false;
}
}

$accepted = class_exists($potentialClassInterfaceOrTrait)
|| interface_exists($potentialClassInterfaceOrTrait)
|| trait_exists($potentialClassInterfaceOrTrait);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\Php\ClassStringToClassConstantFixer;

use Symplify\EasyCodingStandardTester\Testing\AbstractCheckerTestCase;

/**
* @see \Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer
*/
final class AllowedClassesTest extends AbstractCheckerTestCase
{
public function testWrongToFixed(): void
{
$this->doTestCorrectFile(__DIR__ . '/correct/correct2.php.inc');
}

protected function provideConfig(): string
{
return __DIR__ . '/config-with-allowed-classes.yml';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
allow_classes:
- 'DateTimeInterface'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$classType = 'DateTimeInterface';

0 comments on commit 6956252

Please sign in to comment.