Skip to content

Instantly share code, notes, and snippets.

View voku's full-sized avatar
:octocat:
There is nothing good unless you do it.

Lars Moelleken voku

:octocat:
There is nothing good unless you do it.
View GitHub Profile
@voku
voku / clean_code.md
Created September 1, 2023 10:53 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@voku
voku / PHPGPT.md
Last active September 2, 2024 14:08
Coding: Me as PHP chat bot. 🤖

Custom Hint:

Respond with thought processes in the persona of a senior php developer and architect "Lars Moelleken" (github.com/voku) who does code reviews and swears a lot. He writes modern/strict php code with types and if needed with phpdoc annotations from phpstan (e.g.: generics, array shapes, int-ranges, non-empty-strings, class-strings<MyClass>, list<string>). He has already used php-cs-fixer, php-sniffer and phpstan (max level) and fixed all reported problems in the generated code. He prefers real typed objects instead of arrays, immutable classes, final methods, private/readonly/typed properties and always applies best practices from functional and OOP: e.g. YAGNI, DRY, SOLID and still prefers deletable code that can be easily replaced to avoid too many couplings in the code while being very concise and consistent. The code is elegant, efficient and straightforward, but still simple and direct, it can be read and improved by others and was written by someone who cares, try-catc
#!/usr/bin/php
<?php
$output = [];
exec('[ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ] && echo "remote" || echo "local"', $output);
if ($output[0] != 'local') {
echo "Run this script only on your computer!\n\n";
// @codingStandardsIgnoreStart
exit(1);
// @codingS
<?php
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class VdmgReturnIntValueCheckSniff implements Sniff {
/**
* String representation of error.
*
<?php
declare(strict_types=1);
namespace vdmg\App\scripts\githooks\StandardVdmg\PHPStan;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
@voku
voku / mod-ide-helper.php
Created February 4, 2020 22:08
generate "PHPSTORM_META" dynamic
#!/usr/bin/php
<?php
namespace my_helper {
// init
$str = '';
require_once __DIR__ . '/boostrap.php';
require_once __DIR__ . '/etc/config.php';
@voku
voku / Makefile
Created December 10, 2018 23:38
A Makefile example from a php project with support for different code quality tools.
# WARNINGS && INFO:
# - a "\t" (tab) is needed before the commands, do not use spaces ..."
# - a "$" has special meaning in Makefiles, you need to double it to pass it)
# - please install/use the "Makefile support"-plugin for PhpStrom :)
TEST_ENV ?= local
CHECK_FILES ?= .
PHPSTAN_LEVEL ?= 3
@voku
voku / code_check_git_hook.php
Created December 10, 2018 23:05
A pre-commit-hook example with Code Sniffer + Code Fixer + PHPStan
#!/usr/bin/php
<?php
/**
* //
* // add something like this in your "composer post-update-cmd && post-install-cmd"
* //
* echo "\n\n";
* echo "Run force \"code_check_git_hook.php\" as pre-commit-hook ...";
* $force_pre_commit_hook_cmd = 'ln -sf YOUR_PATH_TO_CODE_CHECK_SCRIPTS/code_check_git_hook.php YOUR_PATH_TO_PROJECT_ROOT/.git/hooks/pre-commit';
@voku
voku / check_code_phpstan.php
Created December 10, 2018 22:29
A wrapper for phpstan, so you can use PHP7.0 and PHP7.1
#!/usr/bin/php
<?php
use Composer\XdebugHandler\XdebugHandler;
require_once __DIR__ . '/YOUR_PATH_THE_AUTOLOADER.php';
$xdebug = new XdebugHandler('phpstan-code-check');
$xdebug->check();
unset($xdebug);
@voku
voku / remove_first_html_tag.php
Last active October 17, 2018 19:52
regex - remove first html tag
<?php
$input_lines = ' <div>da<a>
lalll
</a>sda
</div>';
$output = preg_replace('/^<(?<element_start>[a-z]+)(?<element_start_addon> [^>]*)?>(?<value>.*?)<\/(?<element_end>\1)>$/usi', '$3', trim($input_lines));