fix spacing of data provider

This commit is contained in:
TomasVotruba 2020-03-03 10:03:42 +01:00
parent 3e284b0511
commit 9c92126265
7 changed files with 90 additions and 51 deletions

View File

@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Printer;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareParamTagValueNode;
final class PatternFactory
{
/**
* @var string
*/
private const TYPE_PATTERN = '[\w\\\\\[\]\(\)\{\}\:\?\$\-\,\&|<>\s]+';
public function createSpacePattern(PhpDocTagNode $phpDocTagNode): string
{
$spacePattern = preg_quote($phpDocTagNode->name, '#') . '(?<space>\s+)';
// we have to match exact @param space, in case of multiple @param s
if ($phpDocTagNode->value instanceof AttributeAwareParamTagValueNode) {
/** @var AttributeAwareParamTagValueNode $paramTagValueNode */
$paramTagValueNode = $phpDocTagNode->value;
// type could be changed, so better keep it here
$spacePattern .= self::TYPE_PATTERN;
if ($paramTagValueNode->parameterName !== '') {
$spacePattern .= '\s+';
if ($paramTagValueNode->isReference()) {
$spacePattern .= '&';
}
if ($paramTagValueNode->isVariadic) {
$spacePattern .= '...';
}
$spacePattern .= preg_quote($paramTagValueNode->parameterName);
}
}
return '#' . $spacePattern . '#';
}
}

View File

@ -64,18 +64,18 @@ final class PhpDocInfoPrinter
private $multilineSpaceFormatPreserver;
/**
* @var PatternFactory
* @var SpacePatternFactory
*/
private $patternFactory;
private $spacePatternFactory;
public function __construct(
OriginalSpacingRestorer $originalSpacingRestorer,
MultilineSpaceFormatPreserver $multilineSpaceFormatPreserver,
PatternFactory $patternFactory
SpacePatternFactory $spacePatternFactory
) {
$this->originalSpacingRestorer = $originalSpacingRestorer;
$this->multilineSpaceFormatPreserver = $multilineSpaceFormatPreserver;
$this->patternFactory = $patternFactory;
$this->spacePatternFactory = $spacePatternFactory;
}
/**
@ -356,7 +356,7 @@ final class PhpDocInfoPrinter
private function resolveTagSpaceSeparator(PhpDocTagNode $phpDocTagNode): string
{
$originalContent = $this->phpDocInfo->getOriginalContent();
$spacePattern = $this->patternFactory->createSpacePattern($phpDocTagNode);
$spacePattern = $this->spacePatternFactory->createSpacePattern($phpDocTagNode);
$matches = Strings::match($originalContent, $spacePattern);

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Printer;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareGenericTagValueNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareParamTagValueNode;
final class SpacePatternFactory
{
/**
* @var string
*/
private const TYPE_PATTERN = '[\w\\\\\[\]\(\)\{\}\:\?\$\-\,\&|<>\s]+';
public function createSpacePattern(PhpDocTagNode $phpDocTagNode): string
{
$spacePattern = preg_quote($phpDocTagNode->name, '#') . '(?<space>\s+)';
// we have to match exact @param space, in case of multiple @param s
if ($phpDocTagNode->value instanceof AttributeAwareParamTagValueNode) {
$spacePattern = $this->createSpacePatternForParamTagValueNode($phpDocTagNode->value, $spacePattern);
} elseif ($phpDocTagNode->value instanceof AttributeAwareGenericTagValueNode) {
$originalValue = $phpDocTagNode->value->getAttribute('original_value') ?? $phpDocTagNode->value->value;
// break by line break, to prevent false content positive
$originalValueParts = explode(PHP_EOL, $originalValue);
if (isset($originalValueParts[0])) {
$originalValue = $originalValueParts[0];
}
$spacePattern .= preg_quote($originalValue, '#');
}
return '#' . $spacePattern . '#';
}
private function createSpacePatternForParamTagValueNode(
AttributeAwareParamTagValueNode $attributeAwareParamTagValueNode,
string $spacePattern
): string {
// type could be changed, so better keep it here
$spacePattern .= self::TYPE_PATTERN;
if ($attributeAwareParamTagValueNode->parameterName !== '') {
$spacePattern .= '\s+';
if ($attributeAwareParamTagValueNode->isReference()) {
$spacePattern .= '&';
}
if ($attributeAwareParamTagValueNode->isVariadic) {
$spacePattern .= '...';
}
$spacePattern .= preg_quote($attributeAwareParamTagValueNode->parameterName, '#');
}
return $spacePattern;
}
}

View File

@ -0,0 +1,7 @@
/**
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (https://www.zend.com/)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/

View File

@ -0,0 +1,5 @@
/**
* @see https://book.cakephp.org/2/en/models/associations-linking-models-together.html#relationship-types
* hasOne => one to one
* hasMany => one to many
*/

View File

@ -0,0 +1,4 @@
/**
* @todo decouple to NodeAnalyzer
* Matches array like...
*/

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareGenericTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Rector\AbstractPHPUnitRector;
use Rector\Core\RectorDefinition\CodeSample;
@ -120,7 +121,13 @@ PHP
}
$newMethodName = $this->createNewMethodName($oldMethodName);
$dataProviderTag->value->value = $newMethodName;
// @todo create @dataProvider custom tag!
/** @var AttributeAwareGenericTagValueNode $genericTagValueNode */
$genericTagValueNode = $dataProviderTag->value;
// change value - keep original for format preserving
$genericTagValueNode->setAttribute('original_value', $genericTagValueNode->value);
$genericTagValueNode->value = $newMethodName;
$oldMethodName = trim($oldMethodName, '()');
$newMethodName = trim($newMethodName, '()');