Conditions | 5 |
Paths | 8 |
Total Lines | 13 |
Code Lines | 8 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
45 | function get_server_ip() |
||
46 | { |
||
47 | if (!empty($_SERVER['SERVER_ADDR'])) { |
||
48 | $ip = $_SERVER['SERVER_ADDR']; |
||
49 | } elseif (!empty($_SERVER['SERVER_NAME'])) { |
||
50 | $ip = gethostbyname($_SERVER['SERVER_NAME']); |
||
51 | } else { |
||
52 | // for php-cli(phpunit etc.) |
||
53 | $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname()); |
||
54 | } |
||
55 | |||
56 | return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1'; |
||
57 | } |
||
58 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: