From 402ae01469625034ad2edf723d862f9fe6e51e12 Mon Sep 17 00:00:00 2001 From: sebprt Date: Wed, 13 Sep 2023 10:21:17 +0200 Subject: [PATCH 1/2] Fixed code generated in the extractor to prevent an error if a file do not exists --- src/Builder/Extractor.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index 3a3b3a5..c6a51e3 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -85,12 +85,30 @@ public function getNode(): Node { $arguments = [ new Node\Arg( - value: new Node\Expr\New_( - class: new Node\Name\FullyQualified('SplFileObject'), - args: [ - new Node\Arg($this->filePath), - new Node\Arg(new Node\Scalar\String_('r')), - ], + value: new Node\Expr\Ternary( + cond: new Node\Expr\FuncCall( + name: new Node\Name('file_exists'), + args: [ + new Node\Arg( + value: new Node\Expr\Assign( + var: new Node\Expr\Variable('file'), + expr: $this->filePath, + ) + ), + ], + ), + if: new Node\Expr\New_( + class: new Node\Name\FullyQualified('SplFileObject'), + args: [ + new Node\Arg( + value: new Node\Expr\Variable('file'), + ), + new Node\Arg(new Node\Scalar\String_('r')), + ], + ), + else: new Node\Expr\New_( + class: new Node\Name\FullyQualified('SplTempFileObject'), + ), ), name: new Node\Identifier('file'), ), From 2a0de88e4e82ec4aade6b6b01e0c743b00a8c4c5 Mon Sep 17 00:00:00 2001 From: sebprt Date: Thu, 14 Sep 2023 09:17:49 +0200 Subject: [PATCH 2/2] Ran php-cs-fixer --- src/Builder/Extractor.php | 4 +--- src/Builder/Loader.php | 4 +--- src/Builder/MultipleFilesLoader.php | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Builder/Extractor.php b/src/Builder/Extractor.php index c6a51e3..8f29b67 100644 --- a/src/Builder/Extractor.php +++ b/src/Builder/Extractor.php @@ -11,9 +11,7 @@ final class Extractor implements StepBuilderInterface { private ?Node\Expr $logger = null; - public function __construct(private Node\Expr $filePath, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true) - { - } + public function __construct(private Node\Expr $filePath, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true) {} public function withFilePath(Node\Expr $filePath): self { diff --git a/src/Builder/Loader.php b/src/Builder/Loader.php index 77f9f13..5413488 100644 --- a/src/Builder/Loader.php +++ b/src/Builder/Loader.php @@ -11,9 +11,7 @@ final class Loader implements StepBuilderInterface { private ?Node\Expr $logger = null; - public function __construct(private Node\Expr $filePath, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true, private bool $withNonStandard = false) - { - } + public function __construct(private Node\Expr $filePath, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true, private bool $withNonStandard = false) {} public function withFilePath(Node\Expr $filePath): self { diff --git a/src/Builder/MultipleFilesLoader.php b/src/Builder/MultipleFilesLoader.php index 751c872..c8b34de 100644 --- a/src/Builder/MultipleFilesLoader.php +++ b/src/Builder/MultipleFilesLoader.php @@ -11,9 +11,7 @@ final class MultipleFilesLoader implements StepBuilderInterface { private ?Node\Expr $logger = null; - public function __construct(private Node\Expr $filePath, private Node\Expr $maxLines, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true, private bool $withNonStandard = false) - { - } + public function __construct(private Node\Expr $filePath, private Node\Expr $maxLines, private ?Node\Expr $delimiter = null, private ?Node\Expr $enclosure = null, private ?Node\Expr $escape = null, private ?Node\Expr $columns = null, private bool $safeMode = true, private bool $withNonStandard = false) {} public function withFilePath(Node\Expr $filePath): self {