Skip to content

Commit

Permalink
Merge pull request #2 from jaxwilko/wip/env-lexer
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
LukeTowers authored Aug 9, 2023
2 parents c2a5e0a + 2d27b1d commit 9b5d016
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 142 deletions.
6 changes: 2 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
parameters:
level: 6
reportUnmatchedIgnoredErrors: false
level: 5
paths:
- src
ignoreErrors:
- message: '#Access to an undefined property.*?\$expr#'
path: src/ArrayFile.php
18 changes: 2 additions & 16 deletions src/ArrayFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,11 @@
use Winter\LaravelConfigWriter\Parser\PHPFunction;
use Winter\LaravelConfigWriter\Printer\ArrayPrinter;

class ArrayFile implements DataFileInterface
class ArrayFile extends DataFile implements DataFileInterface
{
const SORT_ASC = 'asc';
const SORT_DESC = 'desc';

/**
* @var Stmt[]|null Abstract syntax tree produced by `PhpParser`
*/
protected ?array $ast = null;

/**
* Lexer for use by `PhpParser`
*/
Expand Down Expand Up @@ -160,6 +155,7 @@ public function set($key, $value = null)
if ($target->value->name->parts[0] !== 'env' || !isset($target->value->args[0])) {
return $this;
}
/* @phpstan-ignore-next-line */
if (isset($target->value->args[0]) && !isset($target->value->args[1])) {
$target->value->args[1] = new Arg($this->makeAstNode($valueType, $value));
}
Expand Down Expand Up @@ -443,14 +439,4 @@ public function render(): string
{
return $this->printer->render($this->ast, $this->lexer) . "\n";
}

/**
* Get currently loaded AST
*
* @return Stmt[]|null
*/
public function getAst()
{
return $this->ast;
}
}
20 changes: 20 additions & 0 deletions src/Contracts/DataFileLexerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Winter\LaravelConfigWriter\Contracts;

interface DataFileLexerInterface
{
public const T_ENV = 'T_ENV';
public const T_VALUE = 'T_VALUE';
public const T_QUOTED_VALUE = 'T_QUOTED_VALUE';
public const T_WHITESPACE = 'T_WHITESPACE';
public const T_COMMENT = 'T_COMMENT';

/**
* Get the ast from array of src lines
*
* @param string $string
* @return array<int, array>
*/
public function parse(string $string): array;
}
13 changes: 13 additions & 0 deletions src/Contracts/DataFilePrinterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Winter\LaravelConfigWriter\Contracts;

interface DataFilePrinterInterface
{
/**
* Transform the ast back to a src file string
* @param array<int, array> $ast
* @return string
*/
public function render(array $ast): string;
}
25 changes: 25 additions & 0 deletions src/DataFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Winter\LaravelConfigWriter;

use Winter\LaravelConfigWriter\Contracts\DataFileInterface;

abstract class DataFile implements DataFileInterface
{
/**
* Abstract syntax tree
*
* @var mixed
*/
protected $ast = [];

/**
* Get currently loaded AST
*
* @return \PhpParser\Node\Stmt[]|array|null
*/
public function getAst()
{
return $this->ast;
}
}
Loading

0 comments on commit 9b5d016

Please sign in to comment.