-
Notifications
You must be signed in to change notification settings - Fork 5
Update property generation for typed properties #14
Changes from all commits
8c3f8ee
eb7b2e9
db50fa7
ae64a77
8ebfe3a
78bc9a2
5e89e90
fe335d2
5b105e8
60f8881
798eda4
7580d42
4d6785e
a668a23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,10 @@ public function indent(string $string, int $level = 0) | |
{ | ||
$lines = explode(PHP_EOL, $string); | ||
$lines = array_map(function ($line) use ($level) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test is missing for this class, but it would be good to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if (empty($line)) { | ||
return $line; | ||
} | ||
|
||
return str_repeat($this->indentation, $level) . $line; | ||
}, $lines); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{% if prototype.type.notNone %} | ||
|
||
/** | ||
* @var {{ prototype.type }} | ||
* @var {{ prototype.type|trim('?', 'left') }}{{ prototype.type.nullable ? '|null' }} | ||
*/ | ||
{% endif %} | ||
{{ prototype.visibility }} ${{ prototype.name}}{% if prototype.defaultValue.notNone %} = {{ prototype.defaultValue.export }}{% endif %}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -359,6 +359,7 @@ class Kitten implements Feline, Infant | |
Type::fromString('PlaneCollection') | ||
), | ||
<<<'EOT' | ||
|
||
/** | ||
* @var PlaneCollection | ||
*/ | ||
|
@@ -487,6 +488,7 @@ public function sleep(); | |
|
||
trait Oryctolagus | ||
{ | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
|
@@ -499,6 +501,7 @@ public function burrow(Depth $depth = 'deep') | |
|
||
class Rabbits extends Leporidae implements Animal | ||
{ | ||
|
||
/** | ||
* @var int | ||
*/ | ||
|
@@ -584,6 +587,7 @@ class Rabbits implements Animal | |
const LEGS = 4; | ||
const SKIN = 'soft'; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be one blank line between member "types" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the same issue than above. |
||
/** | ||
* @var int | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Phpactor\CodeBuilder\Domain\Code; | ||
use Phpactor\CodeBuilder\Domain\Builder\SourceCodeBuilder; | ||
use Phpactor\CodeBuilder\Domain\Prototype\SourceCode; | ||
use Phpactor\WorseReflection\Core\Type; | ||
|
||
abstract class UpdaterTestCase extends TestCase | ||
{ | ||
|
@@ -883,7 +884,7 @@ class Aardvark | |
EOT | ||
]; | ||
|
||
yield 'It adds a documented properties' => [ | ||
yield 'It adds a typed property' => [ | ||
<<<'EOT' | ||
class Aardvark | ||
{ | ||
|
@@ -905,6 +906,33 @@ class Aardvark | |
*/ | ||
public $propertyOne; | ||
} | ||
EOT | ||
]; | ||
|
||
yield 'It adds a nullable typed property' => [ | ||
<<<'EOT' | ||
class Aardvark | ||
{ | ||
public $eyes | ||
} | ||
EOT | ||
, SourceCodeBuilder::create() | ||
->class('Aardvark') | ||
->property('propertyOne')->type( | ||
Type::fromString('Hello')->asNullable() | ||
)->end() | ||
->end() | ||
->build(), | ||
<<<'EOT' | ||
class Aardvark | ||
{ | ||
public $eyes | ||
|
||
/** | ||
* @var Hello|null | ||
*/ | ||
public $propertyOne; | ||
} | ||
EOT | ||
]; | ||
|
||
|
@@ -925,6 +953,7 @@ public function crawl() | |
<<<'EOT' | ||
class Aardvark | ||
{ | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, actually this change is controversial for me and is not the SF coding standard or PSR-12, would rather have no space here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to too :) <?php
class Foo
{
use SomeTrait;
public function __construct($a)
{
}
} And you try to complete the constructor, the property would have been added above the trait declaration, which is not PSR-12 compliant neither. The problem here is that we want to add a blank line only if there is a phpdoc block for the new property. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, only thing I can suggest is making it as consistent as possible, if we trade one bug for another then let's not add this change. The problem in general with this library is that it explicitly couples to the presenatation - one solution might be to write a simple CS/whitespace fixer using the Tolerant Parser AST, which can operate on a range of lines (hmm 🤔 ) We could then care less about whitespace concerns when building the source code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's consistent in the way that it's only in two scenario and only if using phpdoc. And most important it only appends (if I remember correctly), when you have no properties yet. The use of the parser would make it clean of course, if we can manage to check the number of black lines between a declaration member and a function member. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, ok. if the pros outweigh the cons, the let's go with it.
I'll try and have a look at this |
||
* @var Hello | ||
*/ | ||
|
@@ -1092,6 +1121,7 @@ public function crawl() | |
<<<'EOT' | ||
trait Aardvark | ||
{ | ||
|
||
/** | ||
* @var Hello | ||
*/ | ||
|
@@ -1267,6 +1297,35 @@ public function methodOne(Barf $sniff) | |
{ | ||
} | ||
} | ||
EOT | ||
]; | ||
|
||
yield 'It adds nullable typed parameters' => [ | ||
<<<'EOT' | ||
class Aardvark | ||
{ | ||
public function methodOne() | ||
{ | ||
} | ||
} | ||
EOT | ||
, SourceCodeBuilder::create() | ||
->class('Aardvark') | ||
->method('methodOne') | ||
->parameter('sniff')->type( | ||
Type::fromString('Barf')->asNullable() | ||
) | ||
->end() | ||
->end() | ||
->end() | ||
->build(), | ||
<<<'EOT' | ||
class Aardvark | ||
{ | ||
public function methodOne(?Barf $sniff) | ||
{ | ||
} | ||
} | ||
EOT | ||
]; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Phpactor\CodeBuilder\Tests\Unit\Util; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Phpactor\CodeBuilder\Util\TextFormat; | ||
|
||
class TextFormatTest extends TestCase | ||
{ | ||
public function testThatItDoesNotIndentBlankLines() | ||
{ | ||
$input = <<<EOT | ||
Non blank line | ||
|
||
Non blank line | ||
EOT; | ||
$expectedOutput = <<<EOT | ||
Non blank line | ||
|
||
Non blank line | ||
EOT; | ||
|
||
$formater = new TextFormat(' '); | ||
$this->assertSame($expectedOutput, $formater->indent($input, 1)); | ||
} | ||
|
||
public function testThatItIndentOnMoreThanOneLevel() | ||
{ | ||
$input = <<<EOT | ||
Non blank line | ||
|
||
Non blank line | ||
EOT; | ||
$expectedOutput = <<<EOT | ||
Non blank line | ||
|
||
Non blank line | ||
EOT; | ||
|
||
$formater = new TextFormat(' '); | ||
$this->assertSame($expectedOutput, $formater->indent($input, 2)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a test(s) for the these fixes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or is this just presentation? if it's not a functional change then the test is not so important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name it's just presentation.
But the fact that the blank line before new properties was moved to the template it's functional.
Didn't I update the tests already ?