# Category: Coding & Scripting

## PHP: realpath() for non-existing path

The php method realpath() can transform the string format of a path into a real path. Means, a path string like:

/Users/mathias/data/../projects/website


will become:

/Users/mathias/projects/website


But this only works, if the path really exists. For non-existing paths, this function cannot be used. To get the same functionality, the following function can be used:

/**
* Get normalized path, like realpath() for non-existing path or file
*
* @param string $path path to be normalized * @return false|string|string[] */ public function normalizePath(string$path)
{
return array_reduce(explode('/', $path), function($a, $b) { if ($a === null) {
$a = "/"; } if ($b === "" || $b === ".") { return$a;
}
if ($b === "..") { return dirname($a);
}

return preg_replace("/\/+/", "/", "$a/$b");
});
}



## How to ignore PHP_CodeSniffer warning: Line exceeds 120 characters;

When using codesniffer to check your code, a lot of warnings might appear when the lines are too long:

----------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES
----------------------------------------------------------------------
73 | WARNING | Line exceeds 120 characters; contains 162 characters
75 | WARNING | Line exceeds 120 characters; contains 124 characters
102 | WARNING | Line exceeds 120 characters; contains 168 characters
108 | WARNING | Line exceeds 120 characters; contains 121 characters
----------------------------------------------------------------------


To ignore those warnings, we can add // phpcs:ignore as a comment to the end of a (too long) line. For example:

<?php
$message = 'This is my long message. It\'s not only long, it\'s extreeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeemly long'; // phpcs:ignore  ## CocoaPods: pod update/install stuck at “Pre-downloading …” When CocoaPods stops the execution of install or update command, it’s helpful to clean the CocoaPod cache using the following commands: rm -rf "${HOME}/Library/Caches/CocoaPods"
rm -rf "pwd/Pods/"


After this, the update (or install) should run smoothly:

pod update


## Flutter: generating *.g.dart files for json serialization

The full documentation for this is available on flutter.dev

When creating json_serializable classes the first time, you’ll get errors similar to what is shown in the image below.

These errors are entirely normal and are simply because the generated code for the model class does not exist yet. To resolve this, run the code generator that generates the serialization boilerplate.

There are two ways of running the code generator.

### One-time code generation

By running

flutter pub run build_runner build


in the project root, you generate JSON serialization code for your models whenever they are needed. This triggers a one-time build that goes through the source files, picks the relevant ones, and generates the necessary serialization code for them.

While this is convenient, it would be nice if you did not have to run the build manually every time you make changes in your model classes.

### Generating code continuously

watcher makes our source code generation process more convenient. It watches changes in our project files and automatically builds the necessary files when needed. Start the watcher by running

flutter pub run build_runner watch


in the project root.

It is safe to start the watcher once and leave it running in the background.

When Sourcetree keeps asking for password when committing or pushing data to a server, the following solution worked for me:

Go to terminal in your project folder and enter:

git config credential.helper store

git pull


Source (somewhere in between the lines):
libpng warning: Interlace handling should be turned on when using png_read_image