Skip to content

Commit 9b5d016

Browse files
authored
Merge pull request #2 from jaxwilko/wip/env-lexer
Fixes #1
2 parents c2a5e0a + 2d27b1d commit 9b5d016

File tree

11 files changed

+363
-142
lines changed

11 files changed

+363
-142
lines changed

phpstan.neon

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
parameters:
2-
level: 6
2+
reportUnmatchedIgnoredErrors: false
3+
level: 5
34
paths:
45
- src
5-
ignoreErrors:
6-
- message: '#Access to an undefined property.*?\$expr#'
7-
path: src/ArrayFile.php

src/ArrayFile.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020
use Winter\LaravelConfigWriter\Parser\PHPFunction;
2121
use Winter\LaravelConfigWriter\Printer\ArrayPrinter;
2222

23-
class ArrayFile implements DataFileInterface
23+
class ArrayFile extends DataFile implements DataFileInterface
2424
{
2525
const SORT_ASC = 'asc';
2626
const SORT_DESC = 'desc';
2727

28-
/**
29-
* @var Stmt[]|null Abstract syntax tree produced by `PhpParser`
30-
*/
31-
protected ?array $ast = null;
32-
3328
/**
3429
* Lexer for use by `PhpParser`
3530
*/
@@ -160,6 +155,7 @@ public function set($key, $value = null)
160155
if ($target->value->name->parts[0] !== 'env' || !isset($target->value->args[0])) {
161156
return $this;
162157
}
158+
/* @phpstan-ignore-next-line */
163159
if (isset($target->value->args[0]) && !isset($target->value->args[1])) {
164160
$target->value->args[1] = new Arg($this->makeAstNode($valueType, $value));
165161
}
@@ -443,14 +439,4 @@ public function render(): string
443439
{
444440
return $this->printer->render($this->ast, $this->lexer) . "\n";
445441
}
446-
447-
/**
448-
* Get currently loaded AST
449-
*
450-
* @return Stmt[]|null
451-
*/
452-
public function getAst()
453-
{
454-
return $this->ast;
455-
}
456442
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Winter\LaravelConfigWriter\Contracts;
4+
5+
interface DataFileLexerInterface
6+
{
7+
public const T_ENV = 'T_ENV';
8+
public const T_VALUE = 'T_VALUE';
9+
public const T_QUOTED_VALUE = 'T_QUOTED_VALUE';
10+
public const T_WHITESPACE = 'T_WHITESPACE';
11+
public const T_COMMENT = 'T_COMMENT';
12+
13+
/**
14+
* Get the ast from array of src lines
15+
*
16+
* @param string $string
17+
* @return array<int, array>
18+
*/
19+
public function parse(string $string): array;
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Winter\LaravelConfigWriter\Contracts;
4+
5+
interface DataFilePrinterInterface
6+
{
7+
/**
8+
* Transform the ast back to a src file string
9+
* @param array<int, array> $ast
10+
* @return string
11+
*/
12+
public function render(array $ast): string;
13+
}

src/DataFile.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Winter\LaravelConfigWriter;
4+
5+
use Winter\LaravelConfigWriter\Contracts\DataFileInterface;
6+
7+
abstract class DataFile implements DataFileInterface
8+
{
9+
/**
10+
* Abstract syntax tree
11+
*
12+
* @var mixed
13+
*/
14+
protected $ast = [];
15+
16+
/**
17+
* Get currently loaded AST
18+
*
19+
* @return \PhpParser\Node\Stmt[]|array|null
20+
*/
21+
public function getAst()
22+
{
23+
return $this->ast;
24+
}
25+
}

0 commit comments

Comments
 (0)