I'm sorry, but this feels really strange. This uses the first 20 hex characters from a 40 character hash. It's like it "destroys" a valid hash. But why? Why not simply do it like this:

if ( strlen( $string ) > 40 ) {
    return sha1( $string );
}
return $string;