# Names ## Namespace Names Namespaces should be written in PascalCase. ## File Names Code directories should have their name written in PascalCase. Code files should contain only one class and have the name of that class. In case of multiple class definitions in one file, it's name should be the same as the "primary" class name. Non-code directories, non-class and non-code files should be named in lisp-case. ## Variable Names Variable names should be written in camelCase. This also applies to function arguments, class instance names and methods. ## Constant Names Constants are written in SCREAMING_SNAKE_CASE, but should be declared case-insensetive. ## Class Names Classes in OpenVK should belong to `openvk\` namespace and be in the corresponding directory (according to PSR-4). Names of classes should be written in PascalCase. ## Function Names camelCase and snake_case are allowed, but first one is the recommended way. This rule does not apply to class methods, which are written in camelCase only. --- # Coding Rules ## File header All OpenVK files must start with `where("meow", true); $photo = $photos->fetch(); $arr = [ "a" => 10, "bb" => true, ]; # NOT OK $photos = (new Photos)->where("meow", true); $photo = $photos->fetch(); $arr = [ "a" => 10, "bb" => true, ]; ``` ## Tab/Space + **Do not use tabs**. Use spaces, as tabs are defined differently for different editors and printers. + Put one space after a comma and semicolons: `exp(1, 2)` `for($i = 1; $i < 100; $i++)` + Put one space around assignment operators: `$a = 1` + Always put a space around conditional operators: `$a = ($a > $b) ? $a : $b` + Do not put spaces between unary operators and their operands, primary operators and keywords: ```php # OK -$a; $a++; $b[1] = $a; fun($b); if($a) { ... } # NOT OK - $a; $a ++; $b [1] = $a; fun ($b); if ($a) { ... } ``` ## Blank Lines + Use blank lines to create paragraphs in the code or comments to make the code more understandable + Use blank lines before `return` statement if it isn't the only statement in the block + Use blank lines after shorthand if/else/etc ```php # OK if($a) return $x; doSomething(); return "yay"; # NOT OK if($a) return $x; # return must be on separate line doSomething(); # doSomething must be separated by an extra blank line after short if/else return "yay"; # do use blank lines before return statement ``` ## Method/Function Arguments + When all arguments for a function do not fit on one line, try to line up the first argument in each line: ![image](https://user-images.githubusercontent.com/34442450/167248563-21fb01be-181d-48b9-ac0c-dc953c0a12cf.png) + If the argument lists are still too long to fit on the line, you may line up the arguments with the method name instead. ## Maximum characters per line Lines should be no more than 80 characters long. ## Usage of curly braces + Curly braces should be on separate line for class, method, and function definitions. + In loops, if/else, try/catch, switch constructions the opening brace should be on the same line as the operator. + Braces must be ommited if the block contains only one statement **AND** the related blocks are also single statemented. + Nested single-statement+operator blocks must not be surrounded by braces. ```php # OK class A { function doSomethingFunny(): int { return 2; } } if(true) { doSomething(); doSomethingElse(); } else { doSomethingFunny(); } if($a) return false; else doSomething(); foreach($b as $c => $d) if($c == $d) unset($b[$c]); # NOT OK class A { function doSomethingFunny(): int { return 2; } } if(true) { doSomething(); doSomethingElse(); } else doSomethingFunny(); # why? if($a) { return false; } else { doSomething(); } foreach($b as $c => $d) { if($c == $d) unset($b[$c]); } # lmao if($a) { doSomething(); } else doSomethingElse(); ``` ## if/else, try/catch + Operators must not be indented with space from their operands but must have 1-space margin from braces: ```php # OK if($a) { doSomething(); doSomethingElse(); } else if($b) { try { nukeSaintPetersburg('😈'); } finally { return PEACE; } } # NOT OK if ($a) { # do not add space between control flow operator IF and it's operand doSomething(); doSomethingElse(); }elseif($b){ # do add margin from braces; also ELSE and IF should be separate here try{ nukeSaintPetersburg('😈'); }finally{ return PEACE; } } ``` ## Switches + `break` must be on same indentation level as the code of le case (not the case definiton itself) + If there is no need to `break` a comment `# NOTICE falling through` must be places instead ```php # OK switch($a) { case 1: echo $a; break; case 2: echo $a++; # NOTICE falling through default: echo "c"; } # NOT OK switch($a) { case 1: echo $a; break; case 2: echo $a++; default: echo "c"; } ```