リファクタリングの日々

  • before
    • データファイル

id=hoge
key=huga

    • プログラム
  function checkAccount($argSourceFilePath, $argId, $argPassword) {
    $fHandle;
    $strWorkId;
    $strWorkPassword;
    $intResultCode = 0;
    $strLine = "";
    $intDivisionIndex;
    $FIND_ID = "id=";
    $FIND_KEY = "key=";

    if (!($fHandle = fopen($argSourceFilePath,"r"))) {
      return $aryResult;
    }

		while(!feof($fHandle)) {
			// 行を取得
	    $strLine = fgets($fHandle);
			
	    // 分解
	    $intDivisionIndex = strpos($strLine, $FIND_ID);
	    if ($intDivesionIndex < 0) {
	      fclose($fHandle);
	      return $intResultCode;
	    }
	    $intDivisionIndex = strpos($strLine, $FIND_KEY);
	    if ($intDivesionIndex < 0) {
	      fclose($fHandle);
	      return $intResultCode;
	    }
	
	    // 改行コードなどの削除
	    $strLine = str_replace("\n", "", $strLine);
	    $strLine = str_replace("\r", "", $strLine);
	
	    // パスワード部分の取得
	    $strWorkPassword = substr($strLine, $intDivisionIndex);
	    $strLine = str_replace($strWorkPassword, "", $strLine);
	    $strWorkPassword = str_replace($FIND_KEY, "", $strWorkPassword);
	
	    // ID部分の取得
	    $strWorkId = $strLine;
	    $strWorkId = str_replace($FIND_ID, "", $strWorkId);

      if (($strWorkId == $argId) && ($strWorkPassword == $argPassword)) {
        $intResultCode = 1;
        break;
      }
    }

    fclose($fHandle);
    return $intResultCode;
  }
  • after
    • データファイル

hoge
huga

    • プログラム
  function checkAccount($filepath, $id, $password) {
    $file = file($filepath);
    return ($id == trim($file[0]) && $password == trim($file[1])) ? 1 : 0;
  }