Analysis result

Scanned at : 2022-09-21
Analyzed by : Coding Standard PSR12

Your PHP Code Snippet

                    
<?php

    $NumberRomanNumeral = array(
        1 => array(
            "RomanNumeral" => "I",
            "NearestNum" => null
        ),
        5 => array(
            "RomanNumeral" => "V",
            "NearestNum" => 4
        ),
        10 => array(
            "RomanNumeral" => "X",
            "NearestNum" => 9
        ),
        50 => array(
            "RomanNumeral" => "L",
            "NearestNum" => 40
        ),
        100 => array(
            "RomanNumeral" => "C",
            "NearestNum" => 90
        ),
        500 => array(
            "RomanNumeral" => "D",
            "NearestNum" => 400
        ),
        1000 => array(
            "RomanNumeral" => "M",
            "NearestNum" => 900
        ),
    );
    
    function separateNumberIntoUnits($n) {
        $separated = str_split($n,1);
        for ($i = 0;$i <= count($separated)-1; $i++){
            $currentNum = $separated[$i];
            $length = count($separated) - $i;
            $separated[$i] = str_pad($currentNum, $length, "0");
        }
        return $separated;
    }   
    
    function getClosestNum($numToFind){
        //echo($numToFind);
        global $NumberRomanNumeral;
        $closestNum = null;
        $foundKey = null;
        $closestMag = null;
        $differenceArray = array();
        $numLeft = null;
        if ($numToFind == 0) {
            return null;
        }
        foreach($NumberRomanNumeral as $num => $data){
            array_push($differenceArray, $data["NearestNum"]);
        }
        foreach($NumberRomanNumeral as $num => $data){
            $currentIndex = array_search($num,array_keys($NumberRomanNumeral));
            $romanNumeral = $data["RomanNumeral"];
            $differenceToSum = $data["NearestNum"];
            $previousDifferenceToSum = null;

            if ($num != 1){
                $previousDifferenceToSum = $differenceArray[$currentIndex-1];
            }
            $mag = $num - $numToFind;
            if ($mag < 0){
                $mag *= -1;
            }
            if ($numToFind == $num){
                $closestNum = $num;
                $foundKey = $romanNumeral;
                break;
            }elseif($numToFind == $differenceToSum){
                $closestNum = $num;
                $foundKey = $romanNumeral;
                break;
            }
            if ($closestMag == null){
                $closestNum = $num;
                $closestMag = $mag;
                $foundKey = $romanNumeral;
            } else {
                if ($mag < $closestMag && $mag > $previousDifferenceToSum) {
                    $closestNum = $num;
                    $closestMag = $mag;
                    $foundKey = $romanNumeral;
                }
            }
        }
        $numLeft = $numToFind - $closestNum;
        if ($numLeft < 0){
            $numLeft *= -1;
        }
        return array($closestNum, $foundKey, $numLeft);
    }
    
    function num2RomanNumeral($num, &$string){
        if ($num != null || $num != 0){
            $numArray = separateNumberIntoUnits($num);
            foreach($numArray as $splitNum){
                $data = getClosestNum($splitNum);
                if ($data != null){
                    $closestNumber = $data[0];  
                    $foundRomanNumeral = $data[1];
                    $numLeft = $data[2];
                    if ($splitNum > $closestNumber){
                        $string = $string.$foundRomanNumeral;
                        if ($numLeft > 0){
                            num2RomanNumeral($numLeft, $string);
                        }
                    }elseif($splitNum < $closestNumber){
                        if ($numLeft > 0){
                            num2RomanNumeral($numLeft, $string);
                        }
                        $string = $string.$foundRomanNumeral;
                    }else{
                        if ($numLeft > 0){
                            num2RomanNumeral($numLeft, $string);
                        }
                        $string = $string.$foundRomanNumeral;
                    }
                
                }
            }
        }
    }
    
    $romanNumeralText = "";

    num2RomanNumeral(4212, $romanNumeralText);

    echo $romanNumeralText;


?>
                    
                

Like you the result ?

Errors: 54

Warning: 1

Severity Type Message Location
5 WARNING A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 34 and the first side effect is on line 3. Line 1, column 1
5 ERROR End of line character is invalid; expected "\n" but found "\r\n" Line 1, column 1
5 ERROR Whitespace found at end of line Line 33, column 1
5 ERROR Opening brace should be on a new line Line 34, column 42
5 ERROR No space found after comma in argument list Line 35, column 34
5 ERROR Expected 1 space after first semicolon of FOR loop; 0 found Line 36, column 20
5 ERROR Expected at least 1 space before "-"; 0 found Line 36, column 44
5 ERROR Expected at least 1 space after "-"; 0 found Line 36, column 44
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 36, column 52
5 ERROR Whitespace found at end of line Line 42, column 6
5 ERROR Whitespace found at end of line Line 43, column 1
5 ERROR Opening brace should be on a new line Line 44, column 39
5 ERROR Expected 1 space(s) after FOREACH keyword; 0 found Line 55, column 9
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 55, column 53
5 ERROR Expected 1 space(s) after FOREACH keyword; 0 found Line 58, column 9
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 58, column 53
5 ERROR No space found after comma in argument list Line 59, column 46
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 64, column 26
5 ERROR Expected at least 1 space before "-"; 0 found Line 65, column 74
5 ERROR Expected at least 1 space after "-"; 0 found Line 65, column 74
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 68, column 25
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 71, column 35
5 ERROR Expected 1 space after closing brace; 0 found Line 75, column 13
5 ERROR Expected 1 space(s) after ELSEIF keyword; 0 found Line 75, column 14
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 75, column 51
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 80, column 36
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 93, column 25
5 ERROR Whitespace found at end of line Line 98, column 1
5 ERROR Opening brace should be on a new line Line 99, column 46
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 100, column 38
5 ERROR Expected 1 space(s) after FOREACH keyword; 0 found Line 102, column 13
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 102, column 43
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 104, column 34
5 ERROR Whitespace found at end of line Line 105, column 47
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 108, column 51
5 ERROR Expected at least 1 space before "."; 0 found Line 109, column 42
5 ERROR Expected at least 1 space after "."; 0 found Line 109, column 42
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 110, column 41
5 ERROR Expected 1 space after closing brace; 0 found Line 113, column 21
5 ERROR Expected 1 space(s) after ELSEIF keyword; 0 found Line 113, column 22
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 113, column 55
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 114, column 41
5 ERROR Expected at least 1 space before "."; 0 found Line 117, column 42
5 ERROR Expected at least 1 space after "."; 0 found Line 117, column 42
5 ERROR Expected 1 space after closing brace; 0 found Line 118, column 21
5 ERROR Expected 1 space(s) after ELSE keyword; 0 found Line 118, column 22
5 ERROR Expected 1 space(s) after closing parenthesis; found 0 Line 119, column 41
5 ERROR Expected at least 1 space before "."; 0 found Line 122, column 42
5 ERROR Expected at least 1 space after "."; 0 found Line 122, column 42
5 ERROR Blank line found at end of control structure Line 124, column 1
5 ERROR Whitespace found at end of line Line 124, column 1
5 ERROR Whitespace found at end of line Line 129, column 1
5 ERROR Expected 1 newline at end of file; 0 found Line 137, column 1
5 ERROR A closing tag is not permitted at the end of a PHP file Line 137, column 1
5 ERROR Line indented incorrectly; expected 4 spaces, found 0 Line 137, column 1