From a6af01a41425aa494712fb4a6ac4c4daf395bff2 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 19 Feb 2019 08:46:39 +0100 Subject: [PATCH 1/2] [OptionsResolver] Updated some example code to use modern PHP --- components/options_resolver.rst | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/components/options_resolver.rst b/components/options_resolver.rst index e3b2ac56598..20fd9529af8 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -46,27 +46,16 @@ check which options are set:: { $mail = ...; - $mail->setHost(isset($this->options['host']) - ? $this->options['host'] - : 'smtp.example.org'); - - $mail->setUsername(isset($this->options['username']) - ? $this->options['username'] - : 'user'); - - $mail->setPassword(isset($this->options['password']) - ? $this->options['password'] - : 'pa$$word'); - - $mail->setPort(isset($this->options['port']) - ? $this->options['port'] - : 25); + $mail->setHost($this->options['host'] ?? 'smtp.example.org'); + $mail->setUsername($this->options['username'] ?? 'user'); + $mail->setPassword($this->options['password'] ?? 'pa$$word'); + $mail->setPort($this->options['port'] ?? 25); // ... } } -This boilerplate is hard to read and repetitive. Also, the default values of the +This boilerplate code is boring and repetitive. Also, the default values of the options are buried in the business logic of your code. Use the :phpfunction:`array_replace` to fix that:: From 3e4f9d8b9673e58778ee257c1658fb4e45b43bf6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 19 Feb 2019 14:55:30 +0100 Subject: [PATCH 2/2] Reword --- components/options_resolver.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/components/options_resolver.rst b/components/options_resolver.rst index 20fd9529af8..c0059e05260 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -36,7 +36,7 @@ Imagine you have a ``Mailer`` class which has four options: ``host``, } } -When accessing the ``$options``, you need to add a lot of boilerplate code to +When accessing the ``$options``, you need to add some boilerplate code to check which options are set:: class Mailer @@ -55,9 +55,8 @@ check which options are set:: } } -This boilerplate code is boring and repetitive. Also, the default values of the -options are buried in the business logic of your code. Use the -:phpfunction:`array_replace` to fix that:: +Also, the default values of the options are buried in the business logic of your +code. Use the :phpfunction:`array_replace` to fix that:: class Mailer {