1. PHP / Говнокод #6959

    +162

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    public function save() {
            try {
                try {
                    $this->create();
                } catch (Exception $e) {
                    //probably dulplicate
                    $this->update();
                }
            } catch (Exception $e) {
                logger::error($e);
            }
    
        }

    try-catch много не бывает

    super, 15 Июня 2011

    Комментарии (23)
  2. PHP / Говнокод #6956

    +157

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    function date_preobr($date, $ind=0) {
    $date1=substr($date, 8, 2); $date21=substr($date, 5, 2); $date3=substr($date, 0, 4);
    if ($ind==0) $data=$date1.".".$date21.".".$date3;
    elseif ($ind==1) $data=$date1.".".$date21.".".substr($date3, 2, strlen($date3)-2);
    elseif ($ind==2) {switch ($date21) {case "01":$date21="января";break;case "02":$date21="февраля";break;
    case "03":$date21="марта";break;case "04":$date21="апреля";break;case "05":$date21="мая";break;
    case "06":$date21="июня";break;case "07":$date21="июля";break;case "08":$date21="августа";break;
    case "09":$date21="сентября";break;case "10":$date21="октября";break;case "11":$date21="ноября";break;
    case "12":$date21="декабря";break;} $data=$date1." ".$date21." ".$date3;}
    return $data;}

    Были даты, есть дата, будут даты.
    Привожу в первозданном виде.

    De-Luxis, 15 Июня 2011

    Комментарии (13)
  3. PHP / Говнокод #6951

    +154

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    public function setIndex() {
    		$lang = '';
    		switch($this->data['find_in']) {
    			case '1' :
    				$this->indexes = Indexes::COMPANIES.$lang;
    			break;
    
    			case '2' :
    				$this->indexes = Indexes::PRODUCTS.$lang;
    			break;
    
    			case '3' :
    				$this->indexes = Indexes::PROMOS.$lang;
    			break;
    
    			default:
    				$this->indexes = Indexes::COMPANIES.$lang;
    			break;
    		}
    	}

    hellow, 14 Июня 2011

    Комментарии (9)
  4. PHP / Говнокод #6941

    +148

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    <?php
    
    class CodeCounter {
        const MULTILINE_COMMENT = 0x01;
    
        private $dir = null;
        private $ext = null;
    
        public function __construct($dir = '.', $ext = '*') {
            $this->dir = $dir;
            if($ext == '*') {
                $this->ext = "/.*/si";
            } else {
                $e = explode('|', $ext);
                foreach($e as &$ext) {
                    $ext = trim($ext);
                    if($ext == '')
                        unset($ext);
                }
                $r = implode('|', $e);
                $this->ext = "/.*\.({$r})$/si";
            }
    
        }
    
        public function calculate() {
            $lines = 0;
            $args = func_get_args();
            if(count($args) == 0)
                $dir = $this->dir;
            else
                $dir = $args[0];
            if(file_exists($dir) && is_dir($dir)) {
                $list = scandir($dir);
                foreach($list as $item) {
                    if($item == '.' || $item == '..')
                        continue;
                    $fullItem = realpath($dir . DIRECTORY_SEPARATOR . $item);
                    if(is_dir($fullItem)) {
                        $lines += $this->calculate($fullItem);
                    } else {
                        if(preg_match($this->ext, $item)) {
                            echo "Calculating lines in {$fullItem}:  ";
                            $_lines = self::count($fullItem);
                            echo "{$_lines}\n";
                            $lines += $_lines;
                        }
                    }
                }
            }
            return $lines;
        }
    
        private static function count($file) {
            $lines = 0;
            $d = null;
            if(file_exists($file) && ($file = file($file))) {
                foreach($file as $line) {
                    $line = trim($line);
                    if($line == '')
                        continue;
                    if( substr($line, 0, 2) == '//' || //single line comment
                        substr($line, 0, 1) == '#'  || //single line comment
                        substr($line, 0, 2) == '<?' || //php open tag
                        substr($line, 0, 2) == '?>'    //php close tags
                    )
                        continue;
                    if(($pos = strpos('/*', $line)) !== false) {
                        if($pos == 0) {
                            if(strpos('*/', $line, $pos) === false) {
                                $d = self::MULTILINE_COMMENT;
                            }
                        } else {
                            $lines++;
                        }
                        continue;
                    }
                    if($d == self::MULTILINE_COMMENT) {
                        if(strpos('*/', $line) !== false) {
                            $d = null;
                        }
                        continue;
                    }
                    $lines++;
                }
            }
            return $lines;
        }
    }
    
    $counter = new CodeCounter('./amapys', 'php|js');
    $lines = $counter->calculate();
    echo "\nTotal: {$lines} lines\n";

    Автор: POPSuL
    Пхп-шники такие пхп-шники.
    ООП во все поля. Им неведом sed и awk.

    cutwater, 13 Июня 2011

    Комментарии (16)
  5. PHP / Говнокод #6940

    +154

    1. 1
    2. 2
    3. 3
    foreach ($templatedata as $templatedataname=>$templatedatavalue)
    	$$templatedataname = $templatedatavalue;
    include($templatesDir.'/'.$file.'.tpl.php');

    Велошаблонизатор, превращающий пары ключ-значение из массива в локальные переменные шаблона.
    Шаблон - простой php-файл, в нужных местах выводящий полученные значения (реже с какой-либо логикой вроде обработки массива).

    Vindicar, 13 Июня 2011

    Комментарии (29)
  6. PHP / Говнокод #6937

    +167

    1. 1
    $r = $this->client->getBerechneteGrundversorgungsTarifebyPLZundVerbrauchKundenart($this->params);

    По сути не говнокод, но нечитабельность налицо...

    vov4ik, 13 Июня 2011

    Комментарии (41)
  7. PHP / Говнокод #6933

    +164

    1. 1
    2. 2
    3. 3
    foreach ($params as $k => $v) {
                eval('$this->' . $k . ' = $v;');
            }

    Yurik, 12 Июня 2011

    Комментарии (3)
  8. PHP / Говнокод #6931

    +158

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    //Отображеие меню
    	$arr = get("select max(depth) as d from ".DP."docs");
    	$to = $arr[0]['d'];
    
    	$a = get("select * from ".DP."docs where depth='0' order by prior");
    	$arr = $a;
    	for($i=0;$i<=$to;$i++)
    	{
    		$a = get("select * from ".DP."docs where depth='".$i."' order by prior");
    		if(is_array($a))
    		foreach($a as $key=>$value)
    		{
    			$b = array();$af = array();$bf = array();
    			$b = get("select * from ".DP."docs where pid='".$a[$key]['id']."' order by prior");
    			if(!$b)$b = array();
    			$before = true;
    			//поиск в массиве
    			foreach($arr as $key2=>$value2)
    			{
    				if($arr[$key2]['id'] != $a[$key]['id'] and $before) $bf[] = $arr[$key2];
    				if($arr[$key2]['id'] == $a[$key]['id'] ){ $bf[] = $arr[$key2]; $before=false;}
    				if($arr[$key2]['id'] != $a[$key]['id'] and !$before) $af[] = $arr[$key2];
    			}
    			$arr = array_merge($bf,$b,$af);
    		}
    	}

    построение дерева сайта. хотя может я не разобрался, весь код пестрит такими перлами.

    com1, 12 Июня 2011

    Комментарии (23)
  9. PHP / Говнокод #6928

    +171

    1. 1
    2. 2
    3. 3
    4. 4
    while(strlen($_SESSION["log"])) $_SESSION["log"]= substr($_SESSION["log"],0,-1);
            while(strlen($_SESSION["pass"])) $_SESSION["pass"]= substr($_SESSION["pass"],0,-1);
            unset($_SESSION["log"]);
            unset($_SESSION["pass"]);

    govnoacc, 11 Июня 2011

    Комментарии (10)
  10. PHP / Говнокод #6927

    +162

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    function strlen2($str)
    {
    $rus=array('й','ц','у','к','е','н','г','ш','щ','з','х','ъ','ф','ы','в','а','п','р','о','л','д','ж','э','я','ч','с','м','и','т','ь','б','ю','Й','Ц','У','К','Е','Н','Г','Ш','Щ','З','Х','Ъ','Ф','Ы','В','А','П','Р','О','Л','Д','Ж','Э','Я','Ч','С','М','И','Т','Ь','Б','Ю');
    return strlen(str_replace($rus, '0', $str));
    }

    jQuery, 11 Июня 2011

    Комментарии (8)