Skip to content

Commit 0a21390

Browse files
committed
Add typehints and use PHP8 methods
1 parent 154195c commit 0a21390

18 files changed

+79
-97
lines changed

src/Composer.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66

77
class Composer {
88

9-
/** @var string|null */
10-
public $EOL = null;
9+
public ?string $EOL = null;
1110

1211
public function compose(EnvFile $file): string {
1312
$visitor = new Visitor();
1413

1514
$file->visitBlocks($visitor);
1615

1716
$EOL = $this->EOL ?? $file->EOL;
18-
$result = implode($EOL, $visitor->results);
19-
return $result;
17+
return implode($EOL, $visitor->results);
2018
}
2119

2220
}

src/Composer/Visitor.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
class Visitor extends EnvVisitor {
1111

1212
/** @var string[] */
13-
public $results = [];
13+
public array $results = [];
1414

15-
public function visitComment(CommentBlock $block){
15+
public function visitComment(CommentBlock $block): void{
1616
$this->addLine("#".$block->text);
1717
}
1818

19-
public function visitVariable(VariableBlock $block){
19+
public function visitVariable(VariableBlock $block): void {
2020
$keyStr = "{$block->key->leftPad}{$block->key->content}{$block->key->rightPad}";
2121
$valueStr = "{$block->value->leftPad}{$block->value->getContentWithQuotes()}{$block->value->rightPad}";
2222

2323
$this->addLine("$keyStr=$valueStr");
2424
}
2525

26-
public function visitUnknown(UnknownBlock $block){
26+
public function visitUnknown(UnknownBlock $block): void{
2727
$this->addLine($block->content);
2828
}
2929

src/EnvFile.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
namespace EnvEditor;
44

5+
use EnvEditor\EnvFile\Block;
56
use EnvEditor\EnvFile\Visitor;
67
use EnvEditor\EnvFile\EOLType;
78
use EnvEditor\EnvFile\Block\Variable as VariableBlock;
89

910
class EnvFile {
1011

11-
/** @var string */
12-
public $EOL = "\n";
12+
public string $EOL = "\n";
1313

14-
/** @var \EnvEditor\EnvFile\Block[] */
15-
public $blocks = [];
14+
/** @var Block[] */
15+
public array $blocks = [];
1616

1717
function __construct() {
1818
$this->EOL = EOLType::UNIX;
1919
}
2020

21-
public function visitBlocks(Visitor $visitor) {
21+
public function visitBlocks(Visitor $visitor): void
22+
{
2223
foreach($this->blocks as $block) {
2324
$block->visit($visitor);
2425
}
@@ -29,7 +30,7 @@ public function visitBlocks(Visitor $visitor) {
2930
*/
3031
public function getVariableBlocks(): array {
3132
return array_values(array_filter($this->blocks, function($block){
32-
return $block instanceof \EnvEditor\EnvFile\Block\Variable;
33+
return $block instanceof VariableBlock;
3334
}));
3435
}
3536

@@ -43,10 +44,11 @@ public function findVariable(string $key): ?VariableBlock {
4344
return null;
4445
}
4546

46-
public function putVariable(VariableBlock $variable) {
47+
public function putVariable(VariableBlock $variable): void
48+
{
4749
foreach($this->blocks as $i => $block) {
4850

49-
if(!$block instanceof \EnvEditor\EnvFile\Block\Variable) continue;
51+
if(!$block instanceof VariableBlock) continue;
5052

5153
if($block->key->content == $variable->key->content) {
5254
$this->blocks[$i] = $variable;
@@ -72,7 +74,8 @@ public function getValue(string $key): string {
7274
return $variable ? $variable->value->content : "";
7375
}
7476

75-
public function setValue(string $key, string $content) {
77+
public function setValue(string $key, string $content): void
78+
{
7679
$variable = $this->findVariable($key);
7780
if(!$variable) {
7881
$variable = new VariableBlock();

src/EnvFile/Block.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
abstract class Block {
66

7-
abstract public function visit(Visitor $visitor);
7+
abstract public function visit(Visitor $visitor): void;
88

99
}

src/EnvFile/Block/Comment.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
class Comment extends Block {
99

10-
/** @var string */
11-
public $text="";
12-
13-
function __construct($text = "") {
14-
$this->text = $text;
10+
function __construct(public string $text = "") {
1511
}
1612

17-
public function visit(Visitor $visitor) {
13+
public function visit(Visitor $visitor): void {
1814
$visitor->visitComment($this);
1915
}
2016

src/EnvFile/Block/Unknown.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
class Unknown extends Block {
99

10-
/** @var string */
11-
public $content="";
12-
13-
function __construct($content = "") {
14-
$this->content = $content;
10+
function __construct(public string $content = "") {
1511
}
1612

17-
public function visit(Visitor $visitor) {
13+
public function visit(Visitor $visitor): void {
1814
$visitor->visitUnknown($this);
1915
}
2016

src/EnvFile/Block/Variable.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99

1010
class Variable extends Block {
1111

12-
/** @var Key */
13-
public $key;
14-
15-
/** @var Value */
16-
public $value;
12+
public Key $key;
13+
public Value $value;
1714

15+
// TODO: Use property promotion in 8.1
1816
function __construct(Key $key = null, Value $value = null) {
1917
$this->key = $key ?? new Key();
2018
$this->value = $value ?? new Value();
2119
}
2220

23-
public function visit(Visitor $visitor) {
21+
public function visit(Visitor $visitor): void {
2422
$visitor->visitVariable($this);
2523
}
2624

src/EnvFile/Block/Variable/Key.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ class Key {
66

77
use Padded;
88

9-
/** @var string */
10-
public $content="";
11-
12-
function __construct($content = "") {
13-
$this->content = $content;
9+
function __construct(public string $content = "") {
1410
}
1511

1612
public function __toString() {

src/EnvFile/Block/Variable/Padded.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
trait Padded {
66

7-
/** @var string */
8-
public $leftPad="";
9-
10-
/** @var string */
11-
public $rightPad="";
7+
public string $leftPad="";
8+
public string $rightPad="";
129

1310
}

src/EnvFile/Block/Variable/Value.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ class Value {
66

77
use Padded;
88

9-
/** @var string */
10-
public $content="";
9+
public string $content="";
1110

12-
/** @var string */
13-
public $quote="";
11+
public string $quote="";
1412

1513
function __construct(string $content = "") {
1614
$this->setContent($content);
@@ -21,7 +19,8 @@ public function __toString() {
2119
}
2220

2321
/** Adds quotes if necessary */
24-
public function setContent(string $content) {
22+
public function setContent(string $content): void
23+
{
2524
$this->content = $content;
2625

2726
if(preg_match('/\s/s', $content)) {

src/EnvFile/EOLType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace EnvEditor\EnvFile;
44

5-
abstract class EOLType {
5+
final class EOLType {
66

77
const WINDOWS = "\r\n";
88
const UNIX = "\n";

src/EnvFile/Visitor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
abstract class Visitor {
1010

11-
public function visitComment(CommentBlock $block){}
12-
public function visitVariable(VariableBlock $block){}
13-
public function visitUnknown(UnknownBlock $block){}
11+
public function visitComment(CommentBlock $block): void {}
12+
public function visitVariable(VariableBlock $block): void {}
13+
public function visitUnknown(UnknownBlock $block): void {}
1414

1515
}

src/Parser.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
class Parser {
1414

15-
/** @var string|null */
16-
public $EOL = null;
15+
public ?string $EOL = null;
1716

1817
public function parse(string $content): EnvFile {
1918

@@ -97,19 +96,20 @@ public function parse(string $content): EnvFile {
9796

9897
}
9998

100-
if((substr($matchedString, -strlen($file->EOL)) === $file->EOL) || strlen($content) == 0) {
99+
if((str_ends_with($matchedString, $file->EOL)) || strlen($content) == 0) {
101100
$file->blocks[] = new UnknownBlock();
102101
}
103102

104103
return $file;
105104

106105
}
107106

108-
public function detectEOLType(string $content, string $default = EOLType::UNIX) {
107+
public function detectEOLType(string $content, string $default = EOLType::UNIX): string
108+
{
109109

110-
if (strpos($content, EOLType::WINDOWS) !== false) {
110+
if (str_contains($content, EOLType::WINDOWS)) {
111111
return EOLType::WINDOWS;
112-
} elseif (strpos($content, EOLType::UNIX) !== false) {
112+
} elseif (str_contains($content, EOLType::UNIX)) {
113113
return EOLType::UNIX;
114114
}
115115

tests/Unit/Composer/VisitorTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class VisitorTest extends TestCase {
1515

16-
public function testComment() {
16+
public function testComment(): void {
1717
$visitor = new Visitor();
1818

1919
$comment = new CommentBlock();
@@ -23,7 +23,7 @@ public function testComment() {
2323
$this->assertEquals(["#foo"], $visitor->results);
2424
}
2525

26-
public function testVariable() {
26+
public function testVariable(): void {
2727
$visitor = new Visitor();
2828

2929
$variable = new VariableBlock();
@@ -37,7 +37,7 @@ public function testVariable() {
3737
/**
3838
* @depends testVariable
3939
*/
40-
public function testQuotedVariable() {
40+
public function testQuotedVariable(): void {
4141
$visitor = new Visitor();
4242

4343
$variable = new VariableBlock();
@@ -52,7 +52,7 @@ public function testQuotedVariable() {
5252
/**
5353
* @depends testVariable
5454
*/
55-
public function testPaddedVariable() {
55+
public function testPaddedVariable(): void {
5656
$visitor = new Visitor();
5757

5858
$variable = new VariableBlock();

tests/Unit/ComposerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class ComposerTest extends TestCase {
1313

14-
public function testCompose() {
14+
public function testCompose(): void {
1515
$composer = new Composer();
1616
$file = new EnvFile();
1717

@@ -22,7 +22,7 @@ public function testCompose() {
2222
$this->assertEquals("#test1\n#test2", $result);
2323
}
2424

25-
public function testEOL() {
25+
public function testEOL(): void {
2626
$composer = new Composer();
2727
$file = new EnvFile();
2828

tests/Unit/EnvFile/VisitorTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
class VisitorTest extends TestCase {
1313

14-
/** @var EnvFile */
15-
private $file;
14+
private EnvFile $file;
1615

1716
protected function setUp(): void {
1817
$this->file = new EnvFile();
@@ -24,12 +23,12 @@ protected function setUp(): void {
2423
$this->file->blocks = [$b1, $b2, $b3];
2524
}
2625

27-
public function testAllVisited() {
26+
public function testAllVisited(): void {
2827
$visitor = new class extends Visitor {
29-
public $visited = [];
28+
public array $visited = [];
3029

31-
public function visitComment(CommentBlock $block){$this->visited[] = $block;}
32-
public function visitVariable(VariableBlock $block){$this->visited[] = $block;}
30+
public function visitComment(CommentBlock $block): void {$this->visited[] = $block;}
31+
public function visitVariable(VariableBlock $block): void {$this->visited[] = $block;}
3332
};
3433

3534
$this->file->visitBlocks($visitor);

0 commit comments

Comments
 (0)