From 8f778d65ba7495d5b613dbcea418049f0e6f8767 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:53:48 +1000 Subject: [PATCH 01/41] Add composer requirements, configuration and workflow for automated updates via GH actions --- .github/workflows/ci.yml | 15 +++++++++++++++ .gitignore | 5 +++-- .php-cs-fixer.dist.php | 21 --------------------- composer.json | 28 +++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .php-cs-fixer.dist.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a7e347e --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,15 @@ +name: CI + +on: + pull_request: null + +jobs: + Bundle: + name: 'Automated fixes' + uses: nswdpc/ci-files/.github/workflows/bundle.yml@main + secrets: inherit + PHPStan: + name: 'PHPStan (analyse)' + uses: nswdpc/ci-files/.github/workflows/phpstan.yml@main + secrets: inherit + needs: Bundle diff --git a/.gitignore b/.gitignore index 7dba068..62e4929 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ /vendor/ .DS_Store /.php-cs-fixer.cache -/codeception.yml -/tests/codeception/tests/_output/ +/public/ +/composer.lock +node_modules diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index f9b7107..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,21 +0,0 @@ -in(__DIR__); - -$config = new PhpCsFixer\Config(); -return $config->setRules([ - '@PSR2' => true, - 'array_indentation' => true, - 'array_syntax' => ['syntax' => 'short'], - 'blank_line_after_namespace' => true, - 'blank_line_after_opening_tag' => true, - 'full_opening_tag' => true, - 'no_closing_tag' => true, - ]) - ->setIndent(" ") - ->setFinder($finder); diff --git a/composer.json b/composer.json index d732728..8186e8a 100644 --- a/composer.json +++ b/composer.json @@ -31,9 +31,12 @@ "client/static" ] }, - "support": { - "key" : "value" - }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/nswdpc/ci-files.git" + } + ], "require": { "dnadesign/silverstripe-elemental": "^5", "silverstripe/framework" : "^5", @@ -43,9 +46,14 @@ "symbiote/silverstripe-multivaluefield" : "^6" }, "require-dev": { - "phpunit/phpunit": "^9.5", + "cambis/silverstripe-rector": "^0.5.1", "friendsofphp/php-cs-fixer": "^3", - "vlucas/phpdotenv": "^5" + "phpstan/phpstan": "^1", + "phpunit/phpunit": "^9.5", + "rector/rector": "^1", + "syntro/silverstripe-phpstan": "^5", + "vlucas/phpdotenv": "^5", + "nswdpc/ci-files": "*" }, "autoload": { "psr-4": { @@ -60,5 +68,15 @@ "NSWDPC\\Chimple\\Traits\\": "src/Traits/", "NSWDPC\\Chimple\\Tests\\": "tests/" } + }, + "prefer-stable": true, + "minimum-stability": "dev", + "config": { + "allow-plugins": { + "composer/installers": true, + "silverstripe/recipe-plugin": true, + "silverstripe/vendor-plugin": true, + "phpstan/extension-installer": true + } } } From 9c098d01868ebbb2dd5ebb764ff9810432a52d72 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:28:09 +1000 Subject: [PATCH 02/41] (ci) remove secrets: inherit config --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a7e347e..74b52b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,9 +7,7 @@ jobs: Bundle: name: 'Automated fixes' uses: nswdpc/ci-files/.github/workflows/bundle.yml@main - secrets: inherit PHPStan: name: 'PHPStan (analyse)' uses: nswdpc/ci-files/.github/workflows/phpstan.yml@main - secrets: inherit needs: Bundle From 53e5d69cebe65ad37668bdbbab738a0964845289 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:30:40 +1000 Subject: [PATCH 03/41] (phpstan) class referenced with incorrect case --- _config/routes.yml | 2 +- src/Extensions/DisableSecurityTokenExtension.php | 2 +- src/Extensions/SiteConfigExtension.php | 6 +++--- src/Models/MailchimpConfig.php | 2 +- src/Models/MailchimpSubscriber.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/_config/routes.yml b/_config/routes.yml index 58ac1dc..6a32c8d 100644 --- a/_config/routes.yml +++ b/_config/routes.yml @@ -3,7 +3,7 @@ Name: silverstripe-chimple-routes After: - '#coreroutes' --- -Silverstripe\Control\Director: +SilverStripe\Control\Director: rules: 'mc-subscribe/v1': 'NSWDPC\Chimple\Controllers\ChimpleController' --- diff --git a/src/Extensions/DisableSecurityTokenExtension.php b/src/Extensions/DisableSecurityTokenExtension.php index 06919c0..54aeb27 100644 --- a/src/Extensions/DisableSecurityTokenExtension.php +++ b/src/Extensions/DisableSecurityTokenExtension.php @@ -2,7 +2,7 @@ namespace NSWDPC\Chimple\Extensions; -use Silverstripe\Core\Extension; +use SilverStripe\Core\Extension; /** * This extension can be applied at the project level in situations where diff --git a/src/Extensions/SiteConfigExtension.php b/src/Extensions/SiteConfigExtension.php index e9d1459..d014b12 100644 --- a/src/Extensions/SiteConfigExtension.php +++ b/src/Extensions/SiteConfigExtension.php @@ -4,10 +4,10 @@ use NSWDPC\Chimple\Models\MailchimpConfig; -use Silverstripe\ORM\DataExtension; +use SilverStripe\ORM\DataExtension; use SilverStripe\Forms\CheckboxField; -use Silverstripe\Forms\FieldList; -use Silverstripe\Forms\DropdownField; +use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\DropdownField; class SiteConfigExtension extends DataExtension { diff --git a/src/Models/MailchimpConfig.php b/src/Models/MailchimpConfig.php index dbb2e2f..fcae230 100644 --- a/src/Models/MailchimpConfig.php +++ b/src/Models/MailchimpConfig.php @@ -13,7 +13,7 @@ use SilverStripe\Forms\TextField; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\HTMLEditor\HTMLEditorField; -use Silverstripe\ORM\DataObject; +use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Security\PermissionProvider; diff --git a/src/Models/MailchimpSubscriber.php b/src/Models/MailchimpSubscriber.php index 42cc371..a12935d 100644 --- a/src/Models/MailchimpSubscriber.php +++ b/src/Models/MailchimpSubscriber.php @@ -324,7 +324,7 @@ public function getCMSFields() ); // get profile link - $dc = MailChimpConfig::getDataCentre(); + $dc = MailchimpConfig::getDataCentre(); if($dc && $this->SubscribedWebId) { $subscriber_profile_link = "https://{$dc}.admin.mailchimp.com/lists/members/view?id={$this->SubscribedWebId}"; } else { From e605e47e13e289d8cbc7a2a59882040b1f2394d8 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:31:18 +1000 Subject: [PATCH 04/41] (phpstan) interface referenced with incorrect case --- tests/TestSubscribeForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestSubscribeForm.php b/tests/TestSubscribeForm.php index 8bacb3d..76edd78 100644 --- a/tests/TestSubscribeForm.php +++ b/tests/TestSubscribeForm.php @@ -3,7 +3,7 @@ namespace NSWDPC\Chimple\Tests; use NSWDPC\Chimple\Forms\SubscribeForm; -use SilverStripe\Dev\Testonly; +use SilverStripe\Dev\TestOnly; /** * Test subscribe form handling From 102f53bc6ccc763f9ad3d65e39f06c78c7aaf3bd Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:33:35 +1000 Subject: [PATCH 05/41] (phpstan) return can be null --- src/Controllers/ChimpleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index ae59087..7d08ec8 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -146,7 +146,7 @@ public function getSubscriptionForm($useXhr = false) : ?SubscribeForm { * Return a subscription form if it is enabled * @link MailchimpConfig::SubscribeForm */ - public function XhrSubscribeForm() : XhrSubscribeForm + public function XhrSubscribeForm() : ?XhrSubscribeForm { $enabled = MailchimpConfig::isEnabled(); From 056c31b2e42b4606e999f85ac5440d81fc7efcd0 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:38:36 +1000 Subject: [PATCH 06/41] (phpstan) fix missing class UploadField via requirement --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 8186e8a..8bf15d1 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "dnadesign/silverstripe-elemental": "^5", "silverstripe/framework" : "^5", "silverstripe/siteconfig" : "^5", + "silverstripe/asset-admin": "^2", "symbiote/silverstripe-queuedjobs": "^5", "drewm/mailchimp-api" : "^2.5", "symbiote/silverstripe-multivaluefield" : "^6" From 736c7bf96695f6fc2ad0fa073f919f38b6b16329 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:43:17 +1000 Subject: [PATCH 07/41] (phpstan) improvements to subscriber class --- src/Models/MailchimpSubscriber.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Models/MailchimpSubscriber.php b/src/Models/MailchimpSubscriber.php index a12935d..165561c 100644 --- a/src/Models/MailchimpSubscriber.php +++ b/src/Models/MailchimpSubscriber.php @@ -61,7 +61,7 @@ class MailchimpSubscriber extends DataObject implements PermissionProvider /** * Default chr for obfuscation - * @var array + * @var string */ private static $obfuscation_chr = "•"; @@ -76,7 +76,7 @@ class MailchimpSubscriber extends DataObject implements PermissionProvider * attempts to update their subscription * This is a potentially destructive action as it will remove tags added to * a subscriber via other means - * @var string + * @var bool */ private static $remove_subscriber_tags = false; @@ -817,7 +817,7 @@ protected function modifySubscriberTags() : bool { * Retrieve all subscribers marked new and attempt to subscribe them * @return array */ - public static function batch_subscribe($limit = 100, $report_only = false) + public static function batch_subscribe($limit = 100, $report_only = false) : array { $results = []; try { @@ -855,7 +855,7 @@ public static function batch_subscribe($limit = 100, $report_only = false) Logger::log("FAIL: could not batch_subscribe, error=" . $e->getMessage(), 'NOTICE'); } - return false; + return []; } public function canView($member = null) From c47aa923cf39e2aa17db12158098a43bef744e93 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:43:35 +1000 Subject: [PATCH 08/41] (phpstan) add return type, can be null --- src/Models/MailchimpConfig.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Models/MailchimpConfig.php b/src/Models/MailchimpConfig.php index fcae230..129c503 100644 --- a/src/Models/MailchimpConfig.php +++ b/src/Models/MailchimpConfig.php @@ -366,9 +366,8 @@ public function requireDefaultRecords() /** * Use the form provided by the controller * @param bool $force_xhr whether to submit in place via XHR or not, the default is to let the config decide - * @return Form */ - public function SubscribeForm($force_xhr = null) + public function SubscribeForm($force_xhr = null) : ?SubscribeForm { // No form available if not enabled $enabled = self::isEnabled(); From d3e115fa4d43bb4cbbaab29185fdd7d890c91ee9 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:44:33 +1000 Subject: [PATCH 09/41] (phpstan) fix unreachable statement --- src/Controllers/ChimpleController.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 7d08ec8..5682efa 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -70,13 +70,10 @@ public function pageTitle($complete = null) switch($complete) { case 'y': return _t(__CLASS__. '.DEFAULT_TITLE_SUCCESSFUL', 'Thanks for subscribing'); - break; case 'n': return _t(__CLASS__. '.DEFAULT_TITLE_NOT_SUCCESSFUL', 'Sorry, there was an error'); - break; default: return _t(__CLASS__. '.DEFAULT_TITLE', 'Subscribe'); - break; } } From 129268af267dd058f8d9f008ccec777308013622 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:48:20 +1000 Subject: [PATCH 10/41] (phpstan) method return type --- src/Controllers/ChimpleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 5682efa..299b989 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -175,7 +175,7 @@ public function XhrSubscribeForm() : ?XhrSubscribeForm * Return a subscription form if it is enabled * @link MailchimpConfig::SubscribeForm */ - public function SubscribeForm() + public function SubscribeForm() : ?SubscribeForm { $enabled = MailchimpConfig::isEnabled(); From 0ef62360312e6a342c2ae90d7722881f2df36285 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:48:42 +1000 Subject: [PATCH 11/41] (phpstan) exception argument ordering fix --- src/Controllers/ChimpleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 299b989..d8ef1e7 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -436,7 +436,7 @@ public function subscribe($data = [], Form $form = null) $sub->Tags = $mc_config->Tags; $sub_id = $sub->write(); if (!$sub_id) { - throw new RequestException("502", "Bad Gateway"); + throw new RequestException("Bad Gateway", 502); } } From 111223db1be7c278a94c11708fbf8c301b6f37f0 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:49:06 +1000 Subject: [PATCH 12/41] (phpstan) argument should be a string --- src/Controllers/ChimpleController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index d8ef1e7..f2f807c 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -493,7 +493,7 @@ private function xhrError($code = 500, $message = "", $description = "") { $response = new HTTPResponse(); $response->setStatusCode($code); $response->addHeader('Content-Type', 'application/json'); - $response->addHeader('X-Submission-OK', 0); + $response->addHeader('X-Submission-OK', '0'); $response->addHeader('X-Submission-Description', $message); return $response; } @@ -506,7 +506,7 @@ private function xhrSuccess($code = 200, $message = "", $description = "") { $response = new HTTPResponse(); $response->setStatusCode($code); $response->addHeader('Content-Type', 'application/json'); - $response->addHeader('X-Submission-OK', 1); + $response->addHeader('X-Submission-OK', '1'); $response->addHeader('X-Submission-Description', $description); return $response; } From 2d95d3b2f57708eafb4adeb4e68f624d4761d3f5 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:08:02 +1000 Subject: [PATCH 13/41] (phpstan) add missing class usage --- src/Models/MailchimpConfig.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Models/MailchimpConfig.php b/src/Models/MailchimpConfig.php index 129c503..f439a6c 100644 --- a/src/Models/MailchimpConfig.php +++ b/src/Models/MailchimpConfig.php @@ -3,6 +3,7 @@ namespace NSWDPC\Chimple\Models; use NSWDPC\Chimple\Controllers\ChimpleController; +use NSWDPC\Chimple\Forms\SubscribeForm; use NSWDPC\Chimple\Services\Logger; use SilverStripe\Core\Convert; use SilverStripe\Core\Config\Config; From 6ea938706d5320fb1cc20115b0e20fff7ac82e70 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:08:12 +1000 Subject: [PATCH 14/41] (phpstan) add type hint --- src/Models/Elements/ElementChimpleSubscribe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/Elements/ElementChimpleSubscribe.php b/src/Models/Elements/ElementChimpleSubscribe.php index 0c8c4e3..5cd5426 100644 --- a/src/Models/Elements/ElementChimpleSubscribe.php +++ b/src/Models/Elements/ElementChimpleSubscribe.php @@ -3,6 +3,7 @@ namespace NSWDPC\Chimple\Models\Elements; use DNADesign\Elemental\Models\BaseElement; +use NSWDPC\Chimple\Forms\SubscribeForm; use NSWDPC\Chimple\Models\MailchimpConfig; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Assets\Image; @@ -146,9 +147,8 @@ public function getCMSFields() /** * Provide $SubscribeForm for template * When called in the context of the administration area, return null - * @return Form|null */ - public function getSubscribeForm() { + public function getSubscribeForm(): ?SubscribeForm { if(Controller::curr() instanceof LeftAndMain) { return null; From 211ef8a7a17b1a721f80697e3d61cbed15dff5c5 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:12:13 +1000 Subject: [PATCH 15/41] (phpstan) replace strpos with simpler str_contains --- tests/ChimpleConfigTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ChimpleConfigTest.php b/tests/ChimpleConfigTest.php index 25a9305..b24f45c 100644 --- a/tests/ChimpleConfigTest.php +++ b/tests/ChimpleConfigTest.php @@ -173,21 +173,21 @@ public function testSubscribeFormTemplateVariable() { // Use config value $template = MailchimpConfig::get_chimple_subscribe_form($config->Code, null); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") === false, "Attribute is not in template"); + $this->assertTrue( !str_contains($template, "data-xhr=\"1\""), "Attribute is not in template"); $config->UseXHR = 1; $config->write(); $template = MailchimpConfig::get_chimple_subscribe_form($config->Code, null); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") !== false, "Attribute is in template"); + $this->assertTrue( str_contains($template, "data-xhr=\"1\""), "Attribute is in template"); // template override $config->UseXHR = 0; $config->write(); $template = MailchimpConfig::get_chimple_subscribe_form($config->Code, '1'); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") !== false, "Attribute is in template"); + $this->assertTrue( str_contains($template, "data-xhr=\"1\""), "Attribute is in template"); $config->UseXHR = 0; $config->write(); $template = MailchimpConfig::get_chimple_subscribe_form($config->Code, '0'); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") === false, "Attribute is not in template"); + $this->assertTrue( !str_contains($template, "data-xhr=\"1\""), "Attribute is not in template"); } public function testGlobalSubscribeFormTemplateVariable() { From 5a90595897145538b808297b9b70720644eefb63 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:20:23 +1000 Subject: [PATCH 16/41] (phpstan) replace strpos with simpler str_contains --- tests/ChimpleConfigTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ChimpleConfigTest.php b/tests/ChimpleConfigTest.php index b24f45c..e279202 100644 --- a/tests/ChimpleConfigTest.php +++ b/tests/ChimpleConfigTest.php @@ -133,7 +133,7 @@ public function testConfiguration() $this->assertTrue( $static_form instanceof DBHTMLText, "Static form for code {$code_value} was not returned"); $needle = " value=\"{$code_value}\" "; - $this->assertTrue( strpos($static_form->forTemplate(), $needle) !== false, "Missing {$code_value} input from form HTML"); + $this->assertTrue( str_contains($static_form->forTemplate(), $needle), "Missing {$code_value} input from form HTML"); } @@ -196,10 +196,10 @@ public function testGlobalSubscribeFormTemplateVariable() { $config->write(); // Use config value $template = MailchimpConfig::get_chimple_global_subscribe_form(); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") === false, "Attribute is not in template"); + $this->assertTrue( !str_contains($template, "data-xhr=\"1\""), "Attribute is not in template"); $config->UseXHR = 1; $config->write(); $template = MailchimpConfig::get_chimple_global_subscribe_form(); - $this->assertTrue( strpos($template, "data-xhr=\"1\"") !== false, "Attribute is in template"); + $this->assertTrue( str_contains($template, "data-xhr=\"1\""), "Attribute is in template"); } } From bb63a8cc5d04e662921263560e80b33454aa8041 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:20:34 +1000 Subject: [PATCH 17/41] (phpstan) remove comments --- src/Controllers/ChimpleController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index f2f807c..28396ed 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -141,7 +141,6 @@ public function getSubscriptionForm($useXhr = false) : ?SubscribeForm { /** * Return a subscription form if it is enabled - * @link MailchimpConfig::SubscribeForm */ public function XhrSubscribeForm() : ?XhrSubscribeForm { @@ -173,7 +172,6 @@ public function XhrSubscribeForm() : ?XhrSubscribeForm /** * Return a subscription form if it is enabled - * @link MailchimpConfig::SubscribeForm */ public function SubscribeForm() : ?SubscribeForm { From bd82c4ecbc7cd86ba9b2c605771085547e8b8ea4 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:22:55 +1000 Subject: [PATCH 18/41] (phpstan) add union return type --- src/Controllers/ChimpleController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 28396ed..0e1beb3 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -130,7 +130,7 @@ public function getFormNameSuffix() : string { /** * Get a subscription form based on parameters */ - public function getSubscriptionForm($useXhr = false) : ?SubscribeForm { + public function getSubscriptionForm($useXhr = false) : SubscribeForm|XhrSubscribeForm|null { if($useXhr) { $form = $this->XhrSubscribeForm(); } else { From 59fbccabfe9fb4c26131d92103f11d6970f9fc3d Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:34:19 +1000 Subject: [PATCH 19/41] (phpstan) improve message/email/config handling --- src/Controllers/ChimpleController.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 0e1beb3..0c1af12 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -329,13 +329,14 @@ public function subscribe($data = [], Form $form = null) $response = null; $code = "";// MailchimpConfig.Code $list_id = ""; + $mc_config = null; + $error_message = ""; + $email = $data['Email'] ?? ''; if(!$form) { throw new RequestException("Forbidden", 403); } - $mc_config = null; - if(empty($data['code'])) { // fail with error $error_message = _t( @@ -349,7 +350,6 @@ public function subscribe($data = [], Form $form = null) $code = strip_tags(trim($data['code'] ?: '')); $error_message = ""; $error_code = 400;// default to invalid data - $mc_config = null; } @@ -363,14 +363,13 @@ public function subscribe($data = [], Form $form = null) // proceed with Email checking... if (!$error_message) { - if (empty($data['Email'])) { + if ($email === '') { // fail with error $error_message = _t( __CLASS__ . '.NO_EMAIL_ADDRESS', "No e-mail address was provided" ); - } - if (!Email::is_valid_address($data['Email'])) { + } elseif (!Email::is_valid_address($email)) { $error_message = _t( __CLASS__ . '.INVALID_EMAIL_ADDRESS', "Please provide a valid e-mail address, '{email}' is not valid", @@ -395,8 +394,9 @@ public function subscribe($data = [], Form $form = null) __CLASS__ . ".GENERIC_ERROR_2", "Sorry, the sign-up could not be completed" ); + } else { + $list_id = $mc_config->getMailchimpListId(); } - $list_id = $mc_config->getMailchimpListId(); } } @@ -421,17 +421,17 @@ public function subscribe($data = [], Form $form = null) ]) // for the Email or the MD5 of it ->filterAny([ - 'Email' => $data['Email'],// match on email address provided - 'SubscribedId' => MailchimpSubscriber::getMailchimpSubscribedId($data['Email'])// OR may not have the email anymore + 'Email' => $email,// match on email address provided + 'SubscribedId' => MailchimpSubscriber::getMailchimpSubscribedId($email)// OR may not have the email anymore ])->first(); if (empty($sub->ID)) { $sub = MailchimpSubscriber::create(); - $sub->Name = $data['Name']; - $sub->Email = $data['Email']; + $sub->Name = $data['Name'] ?? ''; + $sub->Email = $email; $sub->MailchimpListId = $list_id;//list they are subscribing to $sub->Status = MailchimpSubscriber::CHIMPLE_STATUS_NEW; - $sub->Tags = $mc_config->Tags; + $sub->Tags = $mc_config ? $mc_config->Tags : null; $sub_id = $sub->write(); if (!$sub_id) { throw new RequestException("Bad Gateway", 502); From 650ff597d7ae2ba42d7d5352e540ef88fab8f86f Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:44:26 +1000 Subject: [PATCH 20/41] (phpstan) add return types --- src/Models/MailchimpConfig.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Models/MailchimpConfig.php b/src/Models/MailchimpConfig.php index f439a6c..b2431a4 100644 --- a/src/Models/MailchimpConfig.php +++ b/src/Models/MailchimpConfig.php @@ -173,7 +173,7 @@ public static function getGlobalConfig() { /** * If not configured in the database, return the value in yml */ - public function getMailchimpListId() + public function getMailchimpListId(): ?string { $list_id = $this->getField('MailchimpListId'); if (!$list_id) { @@ -187,7 +187,7 @@ public function HasMailchimpListId() return $this->getMailchimpListId() != ''; } - public static function getConfig($id = '', $list_id = '', $code = '') + public static function getConfig($id = '', $list_id = '', $code = ''): ?self { if ($id) { return MailchimpConfig::get()->byId($id); @@ -198,7 +198,7 @@ public static function getConfig($id = '', $list_id = '', $code = '') if ($code) { return MailchimpConfig::get()->filter('Code', $code)->first(); } - return false; + return null; } /** From c8894c482bf712460c670ec949cd7a64e5089471 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:45:02 +1000 Subject: [PATCH 21/41] (phpstan) update handling of code and config --- src/Controllers/ChimpleController.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 0c1af12..792f75a 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -327,7 +327,7 @@ public function subscribe($data = [], Form $form = null) try { $response = null; - $code = "";// MailchimpConfig.Code + $code = strip_tags(trim($data['code'] ?? ''));// MailchimpConfig.Code $list_id = ""; $mc_config = null; $error_message = ""; @@ -337,20 +337,16 @@ public function subscribe($data = [], Form $form = null) throw new RequestException("Forbidden", 403); } - if(empty($data['code'])) { + if($code === "") { // fail with error $error_message = _t( __CLASS__ . '.NO_CODE', "No code was provided" ); $error_code = 400;// default to invalid data - } else { - - $code = strip_tags(trim($data['code'] ?: '')); $error_message = ""; $error_code = 400;// default to invalid data - } $enabled = MailchimpConfig::isEnabled(); @@ -389,13 +385,13 @@ public function subscribe($data = [], Form $form = null) ); } else { $mc_config = MailchimpConfig::getConfig('', '', $code); - if (empty($mc_config->ID)) { + if ($mc_config) { + $list_id = $mc_config->getMailchimpListId(); + } else { $error_message = _t( __CLASS__ . ".GENERIC_ERROR_2", "Sorry, the sign-up could not be completed" ); - } else { - $list_id = $mc_config->getMailchimpListId(); } } } From 9af709fa7dd53570cbc2fe20e48e39817b2f5ff1 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:49:55 +1000 Subject: [PATCH 22/41] (phpstan) return types --- src/Extensions/PageExtension.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Extensions/PageExtension.php b/src/Extensions/PageExtension.php index 40f5463..993eee3 100644 --- a/src/Extensions/PageExtension.php +++ b/src/Extensions/PageExtension.php @@ -2,6 +2,7 @@ namespace NSWDPC\Chimple\Extensions; +use NSWDPC\Chimple\Forms\SubscribeForm; use NSWDPC\Chimple\Models\MailchimpConfig; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Core\Config\Config; @@ -13,9 +14,8 @@ class PageExtension extends Extension /** * Returns a form for the configuration marked 'IsGlobal' - * @return Form|null */ - public function ChimpleGlobalSubscribeForm() { + public function ChimpleGlobalSubscribeForm(): ?SubscribeForm { $config = MailchimpConfig::getGlobalConfig(); if ($config) { return $config->SubscribeForm(); @@ -25,10 +25,9 @@ public function ChimpleGlobalSubscribeForm() { /** * Returns a form based on a config code - * @return Form|null * @param string $config_code a MailchimpConfig.Code value (not an audience ID) */ - public function ChimpleSubscribeForm($config_code) + public function ChimpleSubscribeForm(string $config_code): ?SubscribeForm { $config = MailchimpConfig::getConfig('', '', $config_code); if($config) { From e255ffb413da7f804c568651f1dc305e18cf63a6 Mon Sep 17 00:00:00 2001 From: "James (DPC)" <69664712+JamesDPC@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:55:54 +1000 Subject: [PATCH 23/41] (phpstan) return types update --- src/Controllers/ChimpleController.php | 8 ++------ src/Models/MailchimpConfig.php | 16 +++++----------- src/Models/MailchimpSubscriber.php | 25 ++++++++----------------- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/Controllers/ChimpleController.php b/src/Controllers/ChimpleController.php index 792f75a..3c34d9c 100644 --- a/src/Controllers/ChimpleController.php +++ b/src/Controllers/ChimpleController.php @@ -253,7 +253,6 @@ protected function getActions() : FieldList { /** * Return the default validator for the form. - * @returns Validator|null */ protected function getValidator() : ?Validator { return RequiredFields::create(['Name','Email']); @@ -263,7 +262,6 @@ protected function getValidator() : ?Validator { * Returns the validation callback upon errors * A response is returned only upon errors in XHR submissions * See FormRequestHandler::getValidationErrorResponse(); - * @return callable */ protected function getCallbackForXhrValidation() : callable { return function(ValidationResult $result) { @@ -481,9 +479,8 @@ public function subscribe($data = [], Form $form = null) /** * Return error response for XHR - * @return HTTPResponse */ - private function xhrError($code = 500, $message = "", $description = "") { + private function xhrError($code = 500, $message = "", $description = ""): HTTPResponse { $response = new HTTPResponse(); $response->setStatusCode($code); $response->addHeader('Content-Type', 'application/json'); @@ -494,9 +491,8 @@ private function xhrError($code = 500, $message = "", $description = "") { /** * Return success response for XHR - * @return HTTPResponse */ - private function xhrSuccess($code = 200, $message = "", $description = "") { + private function xhrSuccess($code = 200, $message = "", $description = ""): HTTPResponse { $response = new HTTPResponse(); $response->setStatusCode($code); $response->addHeader('Content-Type', 'application/json'); diff --git a/src/Models/MailchimpConfig.php b/src/Models/MailchimpConfig.php index b2431a4..23692fc 100644 --- a/src/Models/MailchimpConfig.php +++ b/src/Models/MailchimpConfig.php @@ -121,7 +121,6 @@ public static function getApiKey() /** * Returns the data centre (dc) component based on the API key e.g us2 - * @return string */ public static function getDataCentre() : string { $dc = ''; @@ -329,9 +328,8 @@ public function getCMSFields() /** * Return signup link - * @return string */ - public function MailchimpLink() + public function MailchimpLink() : string { return singleton(ChimpleController::class)->Link(); } @@ -405,9 +403,8 @@ public function SubscribeForm($force_xhr = null) : ?SubscribeForm /** * Return alerts for the form - * @return string */ - public function Alerts() + public function Alerts(): string { return '