Skip to content

Commit 1b0cd5a

Browse files
committed
Reformat code
1 parent 0a21390 commit 1b0cd5a

21 files changed

+694
-603
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
max_line_length = 120
10+
tab_width = 4

src/Composer.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
use EnvEditor\Composer\Visitor;
66

7-
class Composer {
7+
class Composer
8+
{
89

9-
public ?string $EOL = null;
10+
public ?string $EOL = null;
1011

11-
public function compose(EnvFile $file): string {
12-
$visitor = new Visitor();
12+
public function compose(EnvFile $file): string
13+
{
14+
$visitor = new Visitor();
1315

14-
$file->visitBlocks($visitor);
16+
$file->visitBlocks($visitor);
1517

16-
$EOL = $this->EOL ?? $file->EOL;
17-
return implode($EOL, $visitor->results);
18-
}
18+
$EOL = $this->EOL ?? $file->EOL;
19+
return implode($EOL, $visitor->results);
20+
}
1921

2022
}

src/Composer/Visitor.php

+25-20
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@
22

33
namespace EnvEditor\Composer;
44

5-
use EnvEditor\EnvFile\Visitor as EnvVisitor;
65
use EnvEditor\EnvFile\Block\Comment as CommentBlock;
7-
use EnvEditor\EnvFile\Block\Variable as VariableBlock;
86
use EnvEditor\EnvFile\Block\Unknown as UnknownBlock;
7+
use EnvEditor\EnvFile\Block\Variable as VariableBlock;
8+
use EnvEditor\EnvFile\Visitor as EnvVisitor;
99

10-
class Visitor extends EnvVisitor {
10+
class Visitor extends EnvVisitor
11+
{
1112

12-
/** @var string[] */
13-
public array $results = [];
13+
/** @var string[] */
14+
public array $results = [];
1415

15-
public function visitComment(CommentBlock $block): void{
16-
$this->addLine("#".$block->text);
17-
}
16+
public function visitComment(CommentBlock $block): void
17+
{
18+
$this->addLine("#" . $block->text);
19+
}
1820

19-
public function visitVariable(VariableBlock $block): void {
20-
$keyStr = "{$block->key->leftPad}{$block->key->content}{$block->key->rightPad}";
21-
$valueStr = "{$block->value->leftPad}{$block->value->getContentWithQuotes()}{$block->value->rightPad}";
21+
private function addLine(string $line)
22+
{
23+
$this->results[] = $line;
24+
}
2225

23-
$this->addLine("$keyStr=$valueStr");
24-
}
26+
public function visitVariable(VariableBlock $block): void
27+
{
28+
$keyStr = "{$block->key->leftPad}{$block->key->content}{$block->key->rightPad}";
29+
$valueStr = "{$block->value->leftPad}{$block->value->getContentWithQuotes()}{$block->value->rightPad}";
2530

26-
public function visitUnknown(UnknownBlock $block): void{
27-
$this->addLine($block->content);
28-
}
31+
$this->addLine("$keyStr=$valueStr");
32+
}
2933

30-
//
34+
//
3135

32-
private function addLine(string $line) {
33-
$this->results[] = $line;
34-
}
36+
public function visitUnknown(UnknownBlock $block): void
37+
{
38+
$this->addLine($block->content);
39+
}
3540

3641
}

src/EnvFile.php

+81-73
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,115 @@
33
namespace EnvEditor;
44

55
use EnvEditor\EnvFile\Block;
6-
use EnvEditor\EnvFile\Visitor;
7-
use EnvEditor\EnvFile\EOLType;
86
use EnvEditor\EnvFile\Block\Variable as VariableBlock;
7+
use EnvEditor\EnvFile\EOLType;
8+
use EnvEditor\EnvFile\Visitor;
9+
10+
class EnvFile
11+
{
912

10-
class EnvFile {
13+
public string $EOL = "\n";
1114

12-
public string $EOL = "\n";
15+
/** @var Block[] */
16+
public array $blocks = [];
1317

14-
/** @var Block[] */
15-
public array $blocks = [];
18+
function __construct()
19+
{
20+
$this->EOL = EOLType::UNIX;
21+
}
1622

17-
function __construct() {
18-
$this->EOL = EOLType::UNIX;
19-
}
23+
public static function loadFrom(string $path): EnvFile
24+
{
25+
$parser = new Parser();
26+
$content = file_get_contents($path);
2027

21-
public function visitBlocks(Visitor $visitor): void
22-
{
23-
foreach($this->blocks as $block) {
24-
$block->visit($visitor);
28+
return $parser->parse($content);
2529
}
26-
}
27-
28-
/**
29-
* @return VariableBlock[]
30-
*/
31-
public function getVariableBlocks(): array {
32-
return array_values(array_filter($this->blocks, function($block){
33-
return $block instanceof VariableBlock;
34-
}));
35-
}
36-
37-
public function findVariable(string $key): ?VariableBlock {
38-
foreach($this->getVariableBlocks() as $block) {
39-
if($block->key == $key) {
40-
return $block;
41-
}
30+
31+
public function visitBlocks(Visitor $visitor): void
32+
{
33+
foreach ($this->blocks as $block) {
34+
$block->visit($visitor);
35+
}
4236
}
4337

44-
return null;
45-
}
38+
public function putVariable(VariableBlock $variable): void
39+
{
40+
foreach ($this->blocks as $i => $block) {
4641

47-
public function putVariable(VariableBlock $variable): void
48-
{
49-
foreach($this->blocks as $i => $block) {
42+
if (!$block instanceof VariableBlock) continue;
5043

51-
if(!$block instanceof VariableBlock) continue;
44+
if ($block->key->content == $variable->key->content) {
45+
$this->blocks[$i] = $variable;
46+
return;
47+
}
48+
}
5249

53-
if($block->key->content == $variable->key->content) {
54-
$this->blocks[$i] = $variable;
55-
return;
56-
}
50+
$this->blocks[] = $variable;
5751
}
5852

59-
$this->blocks[] = $variable;
60-
}
53+
public function removeVariableKey(string $key): ?VariableBlock
54+
{
55+
$variable = $this->findVariable($key);
56+
if (!$variable) return null;
6157

62-
public function removeVariableKey(string $key): ?VariableBlock {
63-
$variable = $this->findVariable($key);
64-
if(!$variable) return null;
58+
$index = array_search($variable, $this->blocks);
59+
array_splice($this->blocks, $index, 1);
60+
return $variable;
61+
}
6562

66-
$index = array_search($variable, $this->blocks);
67-
array_splice($this->blocks, $index, 1);
68-
return $variable;
69-
}
63+
public function findVariable(string $key): ?VariableBlock
64+
{
65+
foreach ($this->getVariableBlocks() as $block) {
66+
if ($block->key == $key) {
67+
return $block;
68+
}
69+
}
7070

71-
public function getValue(string $key): string {
72-
$variable = $this->findVariable($key);
71+
return null;
72+
}
7373

74-
return $variable ? $variable->value->content : "";
75-
}
74+
/**
75+
* @return VariableBlock[]
76+
*/
77+
public function getVariableBlocks(): array
78+
{
79+
return array_values(array_filter($this->blocks, function ($block) {
80+
return $block instanceof VariableBlock;
81+
}));
82+
}
7683

77-
public function setValue(string $key, string $content): void
78-
{
79-
$variable = $this->findVariable($key);
80-
if(!$variable) {
81-
$variable = new VariableBlock();
82-
$variable->key->content = $key;
84+
public function getValue(string $key): string
85+
{
86+
$variable = $this->findVariable($key);
8387

84-
$this->blocks[] = $variable;
88+
return $variable ? $variable->value->content : "";
8589
}
8690

87-
$variable->value->setContent($content);
88-
}
91+
// Utility methods
8992

90-
// Utility methods
93+
public function setValue(string $key, string $content): void
94+
{
95+
$variable = $this->findVariable($key);
96+
if (!$variable) {
97+
$variable = new VariableBlock();
98+
$variable->key->content = $key;
9199

92-
public static function loadFrom(string $path): EnvFile {
93-
$parser = new Parser();
94-
$content = file_get_contents($path);
100+
$this->blocks[] = $variable;
101+
}
95102

96-
return $parser->parse($content);
97-
}
103+
$variable->value->setContent($content);
104+
}
98105

99-
public function saveTo(string $path): void {
100-
$composer = new Composer();
101-
$content = $composer->compose($this);
106+
public function saveTo(string $path): void
107+
{
108+
$composer = new Composer();
109+
$content = $composer->compose($this);
102110

103-
file_put_contents($path, $content);
104-
}
111+
file_put_contents($path, $content);
112+
}
105113

106-
// Private methods
114+
// Private methods
107115

108116

109117
}

src/EnvFile/Block.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace EnvEditor\EnvFile;
44

5-
abstract class Block {
5+
abstract class Block
6+
{
67

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

910
}

src/EnvFile/Block/Comment.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use EnvEditor\EnvFile\Block;
66
use EnvEditor\EnvFile\Visitor;
77

8-
class Comment extends Block {
8+
class Comment extends Block
9+
{
910

10-
function __construct(public string $text = "") {
11-
}
11+
function __construct(public string $text = "")
12+
{
13+
}
1214

13-
public function visit(Visitor $visitor): void {
14-
$visitor->visitComment($this);
15-
}
15+
public function visit(Visitor $visitor): void
16+
{
17+
$visitor->visitComment($this);
18+
}
1619

1720
}

src/EnvFile/Block/Unknown.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use EnvEditor\EnvFile\Block;
66
use EnvEditor\EnvFile\Visitor;
77

8-
class Unknown extends Block {
8+
class Unknown extends Block
9+
{
910

10-
function __construct(public string $content = "") {
11-
}
11+
function __construct(public string $content = "")
12+
{
13+
}
1214

13-
public function visit(Visitor $visitor): void {
14-
$visitor->visitUnknown($this);
15-
}
15+
public function visit(Visitor $visitor): void
16+
{
17+
$visitor->visitUnknown($this);
18+
}
1619

1720
}

src/EnvFile/Block/Variable.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33
namespace EnvEditor\EnvFile\Block;
44

55
use EnvEditor\EnvFile\Block;
6-
use EnvEditor\EnvFile\Visitor;
76
use EnvEditor\EnvFile\Block\Variable\Key;
87
use EnvEditor\EnvFile\Block\Variable\Value;
8+
use EnvEditor\EnvFile\Visitor;
99

10-
class Variable extends Block {
10+
class Variable extends Block
11+
{
1112

12-
public Key $key;
13-
public Value $value;
13+
public Key $key;
14+
public Value $value;
1415

15-
// TODO: Use property promotion in 8.1
16-
function __construct(Key $key = null, Value $value = null) {
17-
$this->key = $key ?? new Key();
18-
$this->value = $value ?? new Value();
19-
}
16+
// TODO: Use property promotion in 8.1
17+
function __construct(Key $key = null, Value $value = null)
18+
{
19+
$this->key = $key ?? new Key();
20+
$this->value = $value ?? new Value();
21+
}
2022

21-
public function visit(Visitor $visitor): void {
22-
$visitor->visitVariable($this);
23-
}
23+
public function visit(Visitor $visitor): void
24+
{
25+
$visitor->visitVariable($this);
26+
}
2427

2528
}

0 commit comments

Comments
 (0)