Skip to content

Commit

Permalink
Code dusting and explicit typing from review
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Mar 16, 2022
1 parent d14374d commit b0e7b4f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/Config/ConfigWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public function toFile(string $filePath, array $newValues): string

public function toContent(string $contents, $newValues): string
{
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer = new Emulative([
$lexer = new Emulative([
'usedAttributes' => [
'comments',
'startTokenPos',
'startLine',
'endTokenPos',
'endLine'
]
]));
]);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer);

try {
$ast = $parser->parse($contents);
Expand Down
11 changes: 3 additions & 8 deletions src/Parse/Contracts/DataFileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@ interface DataFileInterface
/**
* Return a new instance of `DataFileInterface` ready for modification of the provided filepath.
*/
public static function open(string $filePath): ?DataFileInterface;
public static function open(string $filePath): static;

/**
* Set a property within the data.
*
* @param string|array $key
* @param mixed|null $value
*/
public function set($key, $value = null): DataFileInterface;
public function set(string|array $key, $value = null): static;

/**
* Write the current data to a file
*
* @param string|null $filePath
*/
public function write(string $filePath = null): void;
public function write(?string $filePath = null): void;

/**
* Get the printed data
Expand Down
20 changes: 9 additions & 11 deletions src/Parse/EnvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
class EnvFile implements DataFileInterface
{
/**
* @var array Lines of env data
* Lines of env data
*/
protected $env = [];
protected array $env = [];

/**
* @var array Map of variable names to line indexes
* Map of variable names to line indexes
*/
protected $map = [];
protected array $map = [];

/**
* @var string|null Filepath currently being worked on
* Filepath currently being worked on
*/
protected $filePath = null;
protected ?string $filePath = null;

/**
* EnvFile constructor
*/
public function __construct(string $filePath)
final public function __construct(string $filePath)
{
$this->filePath = $filePath;

Expand All @@ -36,7 +36,7 @@ public function __construct(string $filePath)
/**
* Return a new instance of `EnvFile` ready for modification of the file.
*/
public static function open(?string $filePath = null): ?EnvFile
public static function open(?string $filePath = null): static
{
if (!$filePath) {
$filePath = base_path('.env');
Expand All @@ -56,10 +56,8 @@ public static function open(?string $filePath = null): ?EnvFile
* 'DIF_PROPERTY' => 'example'
* ]);
* ```
* @param array|string $key
* @param mixed|null $value
*/
public function set($key, $value = null): EnvFile
public function set(array|string $key, $value = null): static
{
if (is_array($key)) {
foreach ($key as $item => $value) {
Expand Down
38 changes: 15 additions & 23 deletions src/Parse/PHP/ArrayFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ class ArrayFile implements DataFileInterface
/**
* @var Stmt[]|null Abstract syntax tree produced by `PhpParser`
*/
protected $ast = null;
protected ?array $ast = null;

/**
* @var Lexer|null Lexer for use by `PhpParser`
* Lexer for use by `PhpParser`
*/
protected $lexer = null;
protected ?Lexer $lexer = null;

/**
* @var string|null Path to the file
* Path to the file
*/
protected $filePath = null;
protected ?string $filePath = null;

/**
* @var PrettyPrinterAbstract|ArrayPrinter|null Printer used to define output syntax
* Printer used to define output syntax
*/
protected $printer = null;
protected PrettyPrinterAbstract|ArrayPrinter|null $printer = null;

/**
* @var int|null Index of ast containing return stmt
* Index of ast containing return stmt
*/
protected $astReturnIndex = null;
protected ?int $astReturnIndex = null;

/**
* ArrayFile constructor.
*/
public function __construct(array $ast, Lexer $lexer, string $filePath = null, PrettyPrinterAbstract $printer = null)
final public function __construct(array $ast, Lexer $lexer, string $filePath = null, PrettyPrinterAbstract $printer = null)
{
$this->astReturnIndex = $this->getAstReturnIndex($ast);

Expand All @@ -69,23 +69,24 @@ public function __construct(array $ast, Lexer $lexer, string $filePath = null, P
* @throws \InvalidArgumentException if the provided path doesn't exist and $throwIfMissing is true
* @throws SystemException if the provided path is unable to be parsed
*/
public static function open(string $filePath, bool $throwIfMissing = false): ?ArrayFile
public static function open(string $filePath, bool $throwIfMissing = false): static
{
$exists = file_exists($filePath);

if (!$exists && $throwIfMissing) {
throw new \InvalidArgumentException('file not found');
}

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer = new Lexer\Emulative([
$lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startTokenPos',
'startLine',
'endTokenPos',
'endLine'
]
]));
]);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer);

try {
$ast = $parser->parse(
Expand All @@ -111,11 +112,8 @@ public static function open(string $filePath, bool $throwIfMissing = false): ?Ar
* 'property.key2.value' => 'example'
* ]);
* ```
*
* @param string|array $key
* @param mixed|null $value
*/
public function set($key, $value = null): ArrayFile
public function set(string|array $key, $value = null): static
{
if (is_array($key)) {
foreach ($key as $name => $value) {
Expand Down Expand Up @@ -166,10 +164,6 @@ public function set($key, $value = null): ArrayFile

/**
* Creates either a simple array item or a recursive array of items
*
* @param string $key
* @param string $valueType
* @param mixed $value
*/
protected function makeArrayItem(string $key, string $valueType, $value): ArrayItem
{
Expand All @@ -184,8 +178,6 @@ protected function makeArrayItem(string $key, string $valueType, $value): ArrayI
/**
* Generate an AST node, using `PhpParser` classes, for a value
*
* @param string $type
* @param mixed $value
* @throws \RuntimeException If $type is not one of 'string', 'boolean', 'integer', 'function', 'const', 'null', or 'array'
* @return ConstFetch|LNumber|String_|Array_|FuncCall
*/
Expand Down

0 comments on commit b0e7b4f

Please sign in to comment.