Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $finder = PhpCsFixer\Finder::create()
;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'no_unused_imports' => true,
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
],
"require": {
"php": "^7.3 || ^8.0",
"microsoft/tolerant-php-parser": "~0.0.1",
"microsoft/tolerant-php-parser": "~0.1.0",
"phpactor/text-document": "~1.2.3",
"phpactor/worse-reflection": "~0.4.4",
"phpactor/worse-reflection": "~0.4.7",
"twig/twig": "^2.4"
},
"require-dev": {
Expand Down
21 changes: 18 additions & 3 deletions lib/Adapter/TolerantParser/Edits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Phpactor\CodeBuilder\Adapter\TolerantParser;

use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\QualifiedName;
use Microsoft\PhpParser\Token;
use Phpactor\CodeBuilder\Util\TextFormat;
use Phpactor\TextDocument\TextEdit;
use Phpactor\TextDocument\TextEdits;
Expand All @@ -24,24 +27,36 @@ public function __construct(TextFormat $format = null)
;
}

/**
* @param Node|Token $node
*/
public function remove($node): void
{
$this->edits[] = TextEdit::create($node->getFullStart(), $node->getFullWidth(), '');
$this->edits[] = TextEdit::create($node->getFullStartPosition(), $node->getFullWidth(), '');
}

/**
* @param Node|Token $node
*/
public function before($node, string $text): void
{
$this->edits[] = TextEdit::create($node->getStart(), 0, $text);
$this->edits[] = TextEdit::create($node->getStartPosition(), 0, $text);
}

/**
* @param Node|Token $node
*/
public function after($node, string $text): void
{
$this->edits[] = TextEdit::create($node->getEndPosition(), 0, $text);
}

/**
* @param Node|Token|QualifiedName $node
*/
public function replace($node, string $text): void
{
$this->edits[] = TextEdit::create($node->getFullStart(), $node->getFullWidth(), $text);
$this->edits[] = TextEdit::create($node->getFullStartPosition(), $node->getFullWidth(), $text);
}

public function textEdits(): TextEdits
Expand Down
32 changes: 22 additions & 10 deletions lib/Adapter/TolerantParser/Updater/AbstractMethodUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Microsoft\PhpParser\Node\Parameter;
use Phpactor\CodeBuilder\Domain\Prototype\ReturnType;
use Phpactor\TextDocument\TextEdit;
use Phpactor\WorseReflection\Core\Util\QualifiedNameListUtil;

abstract class AbstractMethodUpdater
{
Expand Down Expand Up @@ -59,7 +60,6 @@ public function updateMethods(Edits $edits, ClassLikePrototype $classPrototype,
$methodPrototypes = $classPrototype->methods()->in($existingMethodNames);

$ignoreMethods = [];
/** @var Method $methodBuilder */
foreach ($methodPrototypes as $methodPrototype) {

/** @var MethodDeclaration $methodDeclaration */
Expand Down Expand Up @@ -175,7 +175,18 @@ private function updateOrAddReturnType(Edits $edits, ReturnType $returnType, Met
}

$returnType = (string) $returnType;
$existingReturnType = $returnType ? NodeHelper::resolvedShortName($methodDeclaration, $methodDeclaration->returnType) : null;

if (!$methodDeclaration->returnTypeList) {
return;
}

$firstReturnType = QualifiedNameListUtil::firstQualifiedNameOrNullOrToken($methodDeclaration->returnTypeList);

if (null === $firstReturnType) {
return;
}

$existingReturnType = $returnType ? NodeHelper::resolvedShortName($methodDeclaration, $firstReturnType) : null;

if (null === $existingReturnType) {
// TODO: Add return type
Expand All @@ -186,7 +197,7 @@ private function updateOrAddReturnType(Edits $edits, ReturnType $returnType, Met
return;
}

$edits->replace($methodDeclaration->returnType, ' ' . $returnType);
$edits->replace($firstReturnType, ' ' . $returnType);
}

private function prototypeSameAsDeclaration(Method $methodPrototype, MethodDeclaration $methodDeclaration)
Expand All @@ -199,7 +210,7 @@ private function prototypeSameAsDeclaration(Method $methodPrototype, MethodDecla

/** @var Parameter $parameter */
foreach ($parameters as $parameter) {
$name = ltrim($parameter->variableName->getText($methodDeclaration->getFileContents()), '$');
$name = ltrim((string)$parameter->variableName->getText($methodDeclaration->getFileContents()), '$');

// if method prototype doesn't have the existing parameter
if (false === $methodPrototype->parameters()->has($name)) {
Expand All @@ -211,13 +222,13 @@ private function prototypeSameAsDeclaration(Method $methodPrototype, MethodDecla
$type = $parameterPrototype->type();

// adding a parameter type
if (null === $parameter->typeDeclaration && $type->notNone()) {
if (null === $parameter->typeDeclarationList && $type->notNone()) {
return false;
}

// if parameter has a different type
if (null !== $parameter->typeDeclaration) {
$typeName = $parameter->typeDeclaration->getText($methodDeclaration->getFileContents());
if (null !== $parameter->typeDeclarationList) {
$typeName = $parameter->typeDeclarationList->getText($methodDeclaration->getFileContents());
if ($type->notNone() && (string) $type !== $typeName) {
return false;
}
Expand All @@ -231,13 +242,14 @@ private function prototypeSameAsDeclaration(Method $methodPrototype, MethodDecla
}

// are we adding a return type?
if ($methodPrototype->returnType()->notNone() && null === $methodDeclaration->returnType) {
if ($methodPrototype->returnType()->notNone() && null === $methodDeclaration->returnTypeList) {
return false;
}

// is the return type the same?
if (null !== $methodDeclaration->returnType) {
$name = $methodDeclaration->returnType->getText();
if (null !== $methodDeclaration->returnTypeList) {
// TODO: Does this work?
$name = $methodDeclaration->returnTypeList->getText();
if ($methodPrototype->returnType()->__toString() !== $name) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/TolerantParser/Updater/UseStatementUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function updateUseStatements(Edits $edits, SourceCode $prototype, SourceF

$bodyNode = null;
foreach ($node->getChildNodes() as $childNode) {
if ($childNode->getStart() > $startNode->getStart()) {
if ($childNode->getStartPosition() > $startNode->getStartPosition()) {
$bodyNode = $childNode;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Adapter/TolerantParser/Util/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public static function resolvedShortName(Node $node, $type = null): string
public static function emptyLinesPrecedingNode(Node $node): int
{
$contents = $node->getFileContents();
$preceding = substr($contents, 0, $node->getStart());
$preceding = substr($contents, 0, $node->getStartPosition());

$lines = 0;
$lastChar = null;
for ($i = $node->getStart() - 1; $i > 0; $i--) {
for ($i = $node->getStartPosition() - 1; $i > 0; $i--) {
$char = $contents[$i];

if ($char !== "\n") {
Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Prototype/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter first()
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter get()
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter get(string $name)
*/
class Parameters extends Collection
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/Prototype/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function namespace(): ?string
return null;
}

if (false === strpos($this->type, '\\')) {
if (false === strrpos($this->type, '\\')) {
return null;
}

Expand Down
Loading