From f596ef031a831fab15ca709565cd440ed7ecb3f4 Mon Sep 17 00:00:00 2001 From: dannc Date: Wed, 30 Dec 2020 14:10:44 +0700 Subject: [PATCH] php8-style constuctors --- cli.php | 2 +- src/Command/JokeCommand.php | 9 +++------ src/Operation/JokeOperation.php | 11 ++++------- src/Service/JokeService.php | 8 +++----- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/cli.php b/cli.php index b259674..81bf5c7 100644 --- a/cli.php +++ b/cli.php @@ -6,8 +6,8 @@ $jokeGuzzle = new \GuzzleHttp\Client([ 'base_uri' => 'https://v2.jokeapi.dev', ]); -$jokeService = new \App\Service\JokeService($jokeGuzzle); $cache = new \Sarahman\SimpleCache\FileSystemCache(__DIR__ . '/tmp/cache'); +$jokeService = new \App\Service\JokeService($jokeGuzzle); $jokeOperation = new \App\Operation\JokeOperation($jokeService, $cache); $app = new \Ahc\Cli\Application('Joke App', 'v0.0.1'); diff --git a/src/Command/JokeCommand.php b/src/Command/JokeCommand.php index 03ffd06..59b610b 100644 --- a/src/Command/JokeCommand.php +++ b/src/Command/JokeCommand.php @@ -8,13 +8,10 @@ use App\Operation\JokeOperation; class JokeCommand extends \Ahc\Cli\Input\Command { - private JokeOperation $jokeOperation; - - public function __construct(JokeOperation $jokeOperation) - { + public function __construct( + private JokeOperation $jokeOperation + ) { parent::__construct('joke', 'Get some joke'); - - $this->jokeOperation = $jokeOperation; } public function execute(): void diff --git a/src/Operation/JokeOperation.php b/src/Operation/JokeOperation.php index d13aa91..40f8301 100644 --- a/src/Operation/JokeOperation.php +++ b/src/Operation/JokeOperation.php @@ -10,13 +10,10 @@ use Psr\SimpleCache\CacheInterface; class JokeOperation { - protected JokeInterface $jokeService; - protected CacheInterface $cache; - - public function __construct(JokeInterface $jokeService, CacheInterface $cache) - { - $this->jokeService = $jokeService; - $this->cache = $cache; + public function __construct( + protected JokeInterface $jokeService, + protected CacheInterface $cache + ) { } public function getRandomJoke(): string diff --git a/src/Service/JokeService.php b/src/Service/JokeService.php index c9a5ced..1e061d5 100644 --- a/src/Service/JokeService.php +++ b/src/Service/JokeService.php @@ -12,11 +12,9 @@ use Psr\Http\Message\ResponseInterface; */ class JokeService implements JokeInterface { - protected \GuzzleHttp\Client $guzzleClient; - - public function __construct(\GuzzleHttp\Client $guzzleClient) - { - $this->guzzleClient = $guzzleClient; + public function __construct( + protected \GuzzleHttp\Client $guzzleClient + ) { } #[CachedJokes]