HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Validate profile completeness

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
validatecompletenessprofile

Problem

This function checks these 6 fields for a user profile to determine if they have "completed" their profile. This just feels like it could be done better, but I can't figure how.

/**
 * Is the user's entire profile filled in?
 *
 * @return boolean
 */
public function isProfileComplete()
{
    $checkList = [
        $this->isAddressGiven(),
        $this->isGenderSelected(),
        null !== $this->getFirstName(),
        null !== $this->getLastName(),
        null !== $this->getJobTitle(),
        null !== $this->getAboutMe(),
    ];

    // If any one of the checks fails, profile is missing information
    foreach ($checkList as $checkItem) {
        if (!$checkItem) {
            return false;
        }
    }

    return true;
}

Solution

There's no sense in creating a dynamic array for a fixed set of checks. Just put them in a single boolean expression.

public function isProfileComplete()
{
    return $this->isAddressGiven()
        && $this->isGenderSelected()
        && null !== $this->getFirstName()
        && null !== $this->getLastName()
        && null !== $this->getJobTitle() 
        && null !== $this->getAboutMe()
        ;
}

Code Snippets

public function isProfileComplete()
{
    return $this->isAddressGiven()
        && $this->isGenderSelected()
        && null !== $this->getFirstName()
        && null !== $this->getLastName()
        && null !== $this->getJobTitle() 
        && null !== $this->getAboutMe()
        ;
}

Context

StackExchange Code Review Q#99282, answer score: 4

Revisions (0)

No revisions yet.