It’s very common to see two programmers who code the same feature differently. But it’s much less common for those two programmers to see eye-to-eye and remain friendly.

final class PasswordGenerator implements GeneratorContract { public function __construct( private readonly ListContract $nouns, private readonly ListContract $adjectives, ) { } public function generate(): string { return $this->build( $this->nouns->random(), $this->adjectives->random(), $this->nouns->random(), $this->adjectives->random(), ); } public function generateSecure(): string { return $this->build( $this->nouns->secure(), $this->adjectives->secure(), $this->nouns->secure(), $this->adjectives->secure(), ); } private function build(string...$parts): string { return implode('-', $parts); }}

class PasswordGenerator { public function generate(): string { // generate a password } public function generateSecure(): string { return $this->makeStringSecure($this->generate()); } public function makeStringSecure(string $string): string { return str_replace(['a', 'e', 'i', 'o'], ['4', '3', '1', '0'], $string ); }}

Here’s what our final product looks like: class PasswordGenerator { public function __construct( public readonly array $adjectives, public readonly array $nouns ) { } public function generate(): string { // adjective-noun-adjective-noun return implode('-', [ $this->adjectives[array_rand($this->adjectives)], $this->nouns[array_rand($this->nouns)], $this->adjectives[array_rand($this->adjectives)], $this->nouns[array_rand($this->nouns)], ]); } public function generateSecure(): string { return $this->makeStringSecure($this->generate()); } public function makeStringSecure(string $string): string { return str_replace(['a', 'e', 'i', 'o'], ['4', '3', '1', '0'], $string ); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners