Our Standards And Why We Use Them - CakePHP Version

A presentation at CakeFest Online 2021 in October 2021 in by Christopher Miller

Slide 1

Slide 1

Our Standards And Why We Use Them

Slide 2

Slide 2

Developer at Jump 24 Working with Laravel and CakePHP using TDD WHO AM I? WHAT AM I DOING HERE? Twitter: ccmiller2018 Email: chris@jump24.co.uk Website: jump24.co.uk WE ARE RECRUITING ACROSS THE BOARD REMOTE POSITIONS, MUST BE UK RESIDENT

Slide 3

Slide 3

Why Do We Need Standards? W E H AV E A L O T O F S Y S T E M S W E U S E !

Slide 4

Slide 4

What Is Our Stack? • CakePHP • Sass • Terraform • MySQL • AWS • Redis • PHP • HTML5 • Nginx • CSS3 • Laravel • Javascript • Inertia • Webpack • React • Docker

Slide 5

Slide 5

Key Takeaway STAN D ARD S H E L P - T H EY E NABL E D EVE LO PE R S T O W O RK T O GET H E R BET T E R - SI M PL I F I E S C O D E REVIEW - KEEPS CODE CONSISTENT ACROSS PROJECTS

Slide 6

Slide 6

PHP T H E S TA N D A R D S W E A P P LY

Slide 7

Slide 7

PHP Standards Are Our Centrepiece • Linter • ECS • PHPStan • Tests Everything we do is thought in terms of the “hit by a bus” scenario If a developer were not available tomorrow, could someone else pick up their work?

Slide 8

Slide 8

Linter NOT JUST MAKING IT PRETTY

Slide 9

Slide 9

A Bad Code Example <?php const MY_MESSAGE = ‘something’ function echoMe() { echo (MY_MESSAGE) echo ($otherMessage); } At First Glance This Looks Ok • But there are errors here • Quite a few of them

Slide 10

Slide 10

Our Code Fails To Execute

Slide 11

Slide 11

Bringing In Some Packages “require dev”: { “php parallel lint/php console highlighter”: “^0.5.0”, “php parallel lint/php parallel lint”: “^1.3” }, “scripts”: { “lint”: [ “vendor/bin/parallel lint ./ blame exclude vendor” ] } Two Packages And A Script • Run Composer Install • Run Composer Lint

Slide 12

Slide 12

Now We Can See Whats Wrong

Slide 13

Slide 13

Our Code Fixed <?php const MY_MESSAGE = ‘something’; function echoMe(string $otherMessage) { echo (MY_MESSAGE); echo ($otherMessage); } echoMe(‘else’);

Slide 14

Slide 14

Our Code Executes

Slide 15

Slide 15

Use Our Standards Packages N O W W E S TA RT T O E N F O R C E O U R PA RT I C U L A R C O D I N G S T Y L E

Slide 16

Slide 16

Bringing In All The Packages We Need “require dev”: { “jumptwentyfour/php coding standards”: “^0.0.1”, “jumptwentyfour/project analysers”: “^0.0.2”, “php parallel lint/php console highlighter”: “^0.5.0”, “php parallel lint/php parallel lint”: “^1.3”, },

Slide 17

Slide 17

And The Scripts “scripts”: { “lint”: [ “vendor/bin/parallel lint ./ blame exclude vendor” ], “ecs”: [ “vendor/bin/ecs check” ], “static”: [ “vendor/bin/phpstan analyse memory limit=2G” ], “test”: [ “vendor/bin/pest parallel” ], }

Slide 18

Slide 18

Now A PHPStan.neon le includes: - ./vendor/jumptwentyfour/php coding standards/phpstan.neon - ./phpstan baseline.neon parameters: # The level 8 is the highest level level: 8 excludePaths: - ‘vendor ’ - ‘_ide_helper.php’ - ‘_ide_helper_models.php’ - ‘.phpstorm.meta.php’ checkMissingIterableValueType: false AND DONT FORGET A BLANK PHPSTAN-BASELINE.NEON FILE

Slide 19

Slide 19

And ECS.php <?php use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\EasyCodingStandard\ValueObject\Option; return static function (ContainerConfigurator $containerConfigurator) void { $containerConfigurator import( DIR . ‘/vendor/jumptwentyfour/php coding standards/ecs.php’); $parameters = $containerConfigurator parameters(); $parameters set(Option PATHS, [ DIR . ‘/app’, DIR . ‘/tests’, ]); };

Slide 20

Slide 20

So What Are We Using Here? L I N T I N G , S TAT I C A N A LY S I S , C O D I N G S TA N D A R D S

Slide 21

Slide 21

Why Linting? IT HIGHLIGHTS CODE ERRORS

Slide 22

Slide 22

Why ECS? I T A L L O W S U S T O E N S U R E O U R S TA N D A R D S F O R C O D E A R E M E T, E X T E N D I N G T H I S E A S I LY, A N D AL LOW S FO R I N - E D I T O R H I GH L I GH T I N G O F OU R C U ST OM STAN D ARD S

Slide 23

Slide 23

What Standards Do We Apply? P SR - 1 2 AN D RE L AT E D O T H E R STAN D ARD S , S OM E E X T E RNAL STAN D ARD S SU C H A S S QU I Z L AB S , A S WELL AS SOME OF OUR OWN

Slide 24

Slide 24

Why PHPStan? I T I N T E G R AT E S B ET T E R , A L L O W S F O R S TAT I C A N A LY S I S , H E L P S T O S T R E N GT H E N O U R C O D E

Slide 25

Slide 25

Lets Try This Out WH AT C OUL D G O WRO NG WI T H L IVE C O D E!

Slide 26

Slide 26

This Is Only A Part Of Our Standards We have standards that apply to every single part of our stack. Over time these standards have evolved to cover not just how we code, but how we estimate, how we present to clients, how we manage our projects and almost every other part of our business