Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit d3707df

Browse files
authored
Tolerant parser 0.1 (#34)
* Update to tolerant-parser 0.1 * Remove ignored errors * Fix stan * Update
1 parent 9d73993 commit d3707df

File tree

9 files changed

+182
-199
lines changed

9 files changed

+182
-199
lines changed

.php_cs.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ $finder = PhpCsFixer\Finder::create()
99
;
1010

1111
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
1213
->setRules([
1314
'@PSR2' => true,
1415
'no_unused_imports' => true,

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
],
1111
"require": {
1212
"php": "^7.3 || ^8.0",
13-
"microsoft/tolerant-php-parser": "~0.0.1",
13+
"microsoft/tolerant-php-parser": "~0.1.0",
1414
"phpactor/text-document": "~1.2.3",
15-
"phpactor/worse-reflection": "~0.4.4",
15+
"phpactor/worse-reflection": "~0.4.7",
1616
"twig/twig": "^2.4"
1717
},
1818
"require-dev": {

lib/Adapter/TolerantParser/Edits.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Phpactor\CodeBuilder\Adapter\TolerantParser;
44

5+
use Microsoft\PhpParser\Node;
6+
use Microsoft\PhpParser\Node\QualifiedName;
7+
use Microsoft\PhpParser\Token;
58
use Phpactor\CodeBuilder\Util\TextFormat;
69
use Phpactor\TextDocument\TextEdit;
710
use Phpactor\TextDocument\TextEdits;
@@ -24,24 +27,36 @@ public function __construct(TextFormat $format = null)
2427
;
2528
}
2629

30+
/**
31+
* @param Node|Token $node
32+
*/
2733
public function remove($node): void
2834
{
29-
$this->edits[] = TextEdit::create($node->getFullStart(), $node->getFullWidth(), '');
35+
$this->edits[] = TextEdit::create($node->getFullStartPosition(), $node->getFullWidth(), '');
3036
}
3137

38+
/**
39+
* @param Node|Token $node
40+
*/
3241
public function before($node, string $text): void
3342
{
34-
$this->edits[] = TextEdit::create($node->getStart(), 0, $text);
43+
$this->edits[] = TextEdit::create($node->getStartPosition(), 0, $text);
3544
}
3645

46+
/**
47+
* @param Node|Token $node
48+
*/
3749
public function after($node, string $text): void
3850
{
3951
$this->edits[] = TextEdit::create($node->getEndPosition(), 0, $text);
4052
}
4153

54+
/**
55+
* @param Node|Token|QualifiedName $node
56+
*/
4257
public function replace($node, string $text): void
4358
{
44-
$this->edits[] = TextEdit::create($node->getFullStart(), $node->getFullWidth(), $text);
59+
$this->edits[] = TextEdit::create($node->getFullStartPosition(), $node->getFullWidth(), $text);
4560
}
4661

4762
public function textEdits(): TextEdits

lib/Adapter/TolerantParser/Updater/AbstractMethodUpdater.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Microsoft\PhpParser\Node\Parameter;
1818
use Phpactor\CodeBuilder\Domain\Prototype\ReturnType;
1919
use Phpactor\TextDocument\TextEdit;
20+
use Phpactor\WorseReflection\Core\Util\QualifiedNameListUtil;
2021

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

6162
$ignoreMethods = [];
62-
/** @var Method $methodBuilder */
6363
foreach ($methodPrototypes as $methodPrototype) {
6464

6565
/** @var MethodDeclaration $methodDeclaration */
@@ -175,7 +175,18 @@ private function updateOrAddReturnType(Edits $edits, ReturnType $returnType, Met
175175
}
176176

177177
$returnType = (string) $returnType;
178-
$existingReturnType = $returnType ? NodeHelper::resolvedShortName($methodDeclaration, $methodDeclaration->returnType) : null;
178+
179+
if (!$methodDeclaration->returnTypeList) {
180+
return;
181+
}
182+
183+
$firstReturnType = QualifiedNameListUtil::firstQualifiedNameOrNullOrToken($methodDeclaration->returnTypeList);
184+
185+
if (null === $firstReturnType) {
186+
return;
187+
}
188+
189+
$existingReturnType = $returnType ? NodeHelper::resolvedShortName($methodDeclaration, $firstReturnType) : null;
179190

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

189-
$edits->replace($methodDeclaration->returnType, ' ' . $returnType);
200+
$edits->replace($firstReturnType, ' ' . $returnType);
190201
}
191202

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

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

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

213224
// adding a parameter type
214-
if (null === $parameter->typeDeclaration && $type->notNone()) {
225+
if (null === $parameter->typeDeclarationList && $type->notNone()) {
215226
return false;
216227
}
217228

218229
// if parameter has a different type
219-
if (null !== $parameter->typeDeclaration) {
220-
$typeName = $parameter->typeDeclaration->getText($methodDeclaration->getFileContents());
230+
if (null !== $parameter->typeDeclarationList) {
231+
$typeName = $parameter->typeDeclarationList->getText($methodDeclaration->getFileContents());
221232
if ($type->notNone() && (string) $type !== $typeName) {
222233
return false;
223234
}
@@ -231,13 +242,14 @@ private function prototypeSameAsDeclaration(Method $methodPrototype, MethodDecla
231242
}
232243

233244
// are we adding a return type?
234-
if ($methodPrototype->returnType()->notNone() && null === $methodDeclaration->returnType) {
245+
if ($methodPrototype->returnType()->notNone() && null === $methodDeclaration->returnTypeList) {
235246
return false;
236247
}
237248

238249
// is the return type the same?
239-
if (null !== $methodDeclaration->returnType) {
240-
$name = $methodDeclaration->returnType->getText();
250+
if (null !== $methodDeclaration->returnTypeList) {
251+
// TODO: Does this work?
252+
$name = $methodDeclaration->returnTypeList->getText();
241253
if ($methodPrototype->returnType()->__toString() !== $name) {
242254
return false;
243255
}

lib/Adapter/TolerantParser/Updater/UseStatementUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function updateUseStatements(Edits $edits, SourceCode $prototype, SourceF
3737

3838
$bodyNode = null;
3939
foreach ($node->getChildNodes() as $childNode) {
40-
if ($childNode->getStart() > $startNode->getStart()) {
40+
if ($childNode->getStartPosition() > $startNode->getStartPosition()) {
4141
$bodyNode = $childNode;
4242
break;
4343
}

lib/Adapter/TolerantParser/Util/NodeHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public static function resolvedShortName(Node $node, $type = null): string
5454
public static function emptyLinesPrecedingNode(Node $node): int
5555
{
5656
$contents = $node->getFileContents();
57-
$preceding = substr($contents, 0, $node->getStart());
57+
$preceding = substr($contents, 0, $node->getStartPosition());
5858

5959
$lines = 0;
6060
$lastChar = null;
61-
for ($i = $node->getStart() - 1; $i > 0; $i--) {
61+
for ($i = $node->getStartPosition() - 1; $i > 0; $i--) {
6262
$char = $contents[$i];
6363

6464
if ($char !== "\n") {

lib/Domain/Prototype/Parameters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter first()
7-
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter get()
7+
* @method \Phpactor\CodeBuilder\Domain\Prototype\Parameter get(string $name)
88
*/
99
class Parameters extends Collection
1010
{

lib/Domain/Prototype/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function namespace(): ?string
5353
return null;
5454
}
5555

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

0 commit comments

Comments
 (0)