Colored functions
KPHP brings support of colored functions. It's quite a unique concept that allows you to find undesirable architectural patterns in your code, like “writing logs from performance-critical places is bad” or “deny reading from DB at server-side rendering”.
The concept of colored functions
Historically, this concept was invented for KPHP, but later exposed as a separate project named nocolor.
Colored functions are of course still embedded into KPHP, but for documentation, we'll refer you here:
Visit “Introducing colored functions” page
Differences between KPHP's implementation and nocolor
@kphp-colorinstead of@color- Max 64 unique colors per project (KPHP stores them in a bitmask, not a string list)
- Instead of
palette.yaml, you declare the palette in PHP, in theKphpConfigurationclass
// a separate file KphpConfiguration.php
class KphpConfiguration {
const FUNCTION_PALETTE = [
[
'highload no-highload' => "Don't call a no-highload function from a highload one",
'highload allow-no-highload no-highload' => 1,
],
[
'ssr rpc' => "Don't perform rpc queries from SSR: pass already prepared data",
'ssr ssr-allow-rpc rpc' => 1,
]
];
}
A demo project with colored functions
Save this folder from kphp-snippets somewhere. Execute
kphp2cpp index.php -M cli
Compilation will fail with the message:
fast slow => Potential performance leak
This color rule is broken, call chain:
ApiRequestHandler::handleRequest@fast -> Logger::debug -> DBLayer::addToLogTable@slow
If you
- either remove all
@kphp-colorfrom a demo - or add
@kphp-color slow-ignoreoverLogger::debug()
then compilation will succeed.
An article about colored functions
Here's an article on Habr (in Russian) which also describes this concept: