I have one complex method with 1890 paths. I don't know how can be this refactored. Can somebody add some tips about it? Maybe the MySQL table structure is wrong.
public function fetchClubChallengeState()
{
$c = $this->caller->in_club;
$o = $this->opponent->in_club;
if (!$c || !$o) {
return false;
}
$last = Yii::app()->db->createCommand()
->select('*')
->from('challenge')
->where('(caller=:caller OR caller=:opponent) AND winner=0', [':caller'=>$c, ':opponent'=>$o])
->limit(1)
->queryRow();
if ($last['caller']==$c and $last['opponent']==$o or $last['caller']==$o and $last['opponent']==$c) {
$created = strtotime($last['created']);
if ($this->isBetweenDates($created + 1800, $created + 3600)) {
$this->challengeID = (int)$last['id'];
$this->callersClubRole = $last['caller'] == $c ? 'caller' : 'opponent';
$this->callersClub = $last['caller'] == $c ? $last['name_caller'] : $last['name_opponent'];
$this->opponentsClubRole = $last['opponent'] == $o ? 'opponent' : 'caller';
$this->opponentsClub = $last['opponent'] == $o ? $last['name_opponent'] : $last['name_caller'];
}
}
}
Edit:
The structure of the challenge table:
CREATE TABLE `challenge` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`caller` mediumint(7) unsigned NOT NULL DEFAULT '0',
`opponent` mediumint(7) unsigned NOT NULL DEFAULT '0',
`name_caller` varchar(20) NOT NULL DEFAULT '',
`name_opponent` varchar(20) NOT NULL DEFAULT '',
`winner` tinyint(1) unsigned NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
//...
PRIMARY KEY (`id`),
KEY `c_o` (`caller`,`opponent`),
KEY `winner` (`winner`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
If the challenge was started by Player1's club, that clubs ID goes to the caller
field and the ID of the opponents club goes to the opponent
field. These two IDs are checked here:
if ($last['caller']==$c and $last['opponent']==$o or $last['caller']==$o and $last['opponent']==$c) {
This means, that the found challenge is between the clubs of the 2 players.
The code is also here.
SELECT
that has aLIMIT
but noORDER BY
is rarely useful. Are you sure that your query produces the right result, deterministically? \$\endgroup\$