## Introduction
PHP does not have a native "clamp" (or range constraint) function<sup>(1)</sup>.
As always, there are several ways to resolve this problem.
This repo contains several variations that all achieve the same thing: clamp a given value between a given maximum and minimum range.<sup>(2)</sup>
The question is: _which one has your preference and why?_
It is important to note that there is no right or wrong answer, all of the implementations are fully functional.
the goal is not to see which variation is more popular but to gain an understanding of _why_ a developer prefers one over the other.
## Concerns
When judging code, developers tend to look at the following things<sup>(3)</sup>:
- Amount of code
- Code complexity
- Execution speed
- Low fault tolerance (being bug free)
- Readability
Of these, only "Readability" is subjective. Complexity, fault tolerance, speed and size can all be measured.
- Full details on ammount of code [can be found here](https://gist.github.com/Potherca/0c10a67b12f553a13ebe62f208e97202).
- Full details on execution speed [can be found here](https://gist.github.com/Potherca/b9db0801cc0257715361361c9a05ec78).
- Full details on code complexity [can be found here](https://gist.github.com/Potherca/8379dbf1d3db90ed07665e7e47278e7a).
## Scope
To reduce the feature scope to the absolute minimum, the code in the functions is assumed to be low-level code.
Validation, type coersion, etc are considered out of scope for the sake of this discussion.
At the point `clamp` is called:
- `$value`, `$min` and `$max` are all assumed to be integers
- `$min` is assumed to be smaller than `$max`
---
**Footnotes**
1. See [this issue](https://bugs.php.net/bug.php?id=69862)
2. To avoid the order of the files causing a bias, the filenames contain a partial MD5 hash of their content.
3. Let me know if I missed anything
<!--
@TODO: Add "ruby style"
/**
* @ParamProviders({"provideValues"})
*/
public function bench_($value, $min, $max)
{
$range = [$min, $value, $max];
sort($range, SORT_NUMERIC);
return $range[1];
}
-->