O comportamento diferente da função uasort no PHP 5.5 e PHP 7.0

Eu encontrei um comportamento estranho do Magento 1.8 depois de alterar a versão do php de 5.5 para 7.0. Esse comportamento estranho é devido a uma alteração na função de trabalhouasort.

Código fonte:

<?php

$arr = [
    "nominal" => [
        "before" => ["subtotal", "grand_total"],
        "after" => [],
        "_code" => "nominal"
    ],
    "subtotal" => [
        "after" => ["nominal"],
        "before" => ["grand_total", "shipping", "freeshipping", "tax_subtotal", "discount", "tax", "weee"],
        "_code" => "subtotal"
    ],
    "shipping" => [
        "after" => ["subtotal", "freeshipping", "tax_subtotal", "nominal", "weee"],
        "before" => ["grand_total", "discount", "tax_shipping", "tax"],
        "_code" => "shipping"
    ],
    "grand_total" => [
        "after" => ["subtotal", "nominal", "shipping", "freeshipping", "tax_subtotal", "discount", "tax"],
        "before" => [],
        "_code" => "grand_total"
    ],
    "msrp" => [
        "before" => [],
        "after" => [],
        "_code" => "msrp"
    ],
    "freeshipping" => [
        "after" => ["subtotal", "nominal"],
        "before" => ["tax_subtotal", "shipping", "grand_total", "tax", "discount"],
        "_code" => "freeshipping"
    ],
    "discount" => [
        "after" => ["subtotal", "shipping", "nominal", "freeshipping", "tax_subtotal", "tax_shipping", "weee"],
        "before" => ["grand_total", "tax"],
        "_code" => "discount"
    ],
    "tax_subtotal" => [
        "after" => ["0" => "freeshipping", "1" => "subtotal", "3" => "nominal"],
        "before" => ["tax", "discount", "shipping", "grand_total", "tax_shipping", "weee"],
        "_code" => "tax_subtotal"
    ],
    "tax_shipping" => [
        "after" => ["shipping", "tax_subtotal", "subtotal", "freeshipping", "nominal"],
        "before" => ["tax", "discount", "grand_total"],
        "_code" => "tax_shipping"
    ],
    "tax" => [
        "after" => ["subtotal", "shipping", "discount", "tax_subtotal", "freeshipping", "tax_shipping", "nominal", "weee"],
        "before" => ["grand_total"],
        "_code" => "tax"
    ],
    "weee" => [
        "after" => ["subtotal", "tax_subtotal", "nominal", "freeshipping"],
        "before" => ["shipping", "tax", "discount", "grand_total", "tax_shipping"],
        "_code" => "weee"
    ]
];


function _compareTotals($a, $b)
{
    $aCode = $a['_code'];
    $bCode = $b['_code'];
    if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) {
        $res = -1;
    } elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) {
        $res = 1;
    } else {
        $res = 0;
    }
    echo sprintf("%s <> %s: %s", $aCode, $bCode, $res) . "\n";
    return $res;
}

uasort($arr, '_compareTotals');
var_dump(array_keys($arr));

No php 5.5, o resultado é:

freeshipping <> subtotal: 1
freeshipping <> shipping: -1
weee <> freeshipping: 1
tax <> freeshipping: 1
tax_shipping <> freeshipping: 1
tax_subtotal <> freeshipping: 1
discount <> freeshipping: 1
nominal <> freeshipping: -1
freeshipping <> grand_total: -1
msrp <> freeshipping: 0
subtotal <> msrp: 0
nominal <> subtotal: -1
tax_subtotal <> shipping: -1
weee <> tax_subtotal: 1
tax <> tax_subtotal: 1
tax_shipping <> tax_subtotal: 1
grand_total <> tax_subtotal: 1
discount <> tax_subtotal: 1
shipping <> tax_subtotal: 1
grand_total <> discount: 1
grand_total <> shipping: 1
grand_total <> tax_shipping: 1
grand_total <> tax: 1
weee <> grand_total: -1
shipping <> discount: -1
tax <> shipping: 1
tax_shipping <> shipping: 1
weee <> shipping: -1
tax_shipping <> discount: -1
tax <> tax_shipping: 1
discount <> tax_shipping: 1
tax <> discount: 1

array(11) {
  [0] =>
  string(7) "nominal"
  [1] =>
  string(8) "subtotal"
  [2] =>
  string(4) "msrp"
  [3] =>
  string(12) "freeshipping"
  [4] =>
  string(12) "tax_subtotal"
  [5] =>
  string(4) "weee"
  [6] =>
  string(8) "shipping"
  [7] =>
  string(12) "tax_shipping"
  [8] =>
  string(8) "discount"
  [9] =>
  string(3) "tax"
  [10] =>
  string(11) "grand_total"
}

No php 7.0, o resultado é:

nominal <> subtotal: -1
subtotal <> shipping: -1
shipping <> grand_total: -1
grand_total <> msrp: 0
msrp <> freeshipping: 0
freeshipping <> discount: -1
discount <> tax_subtotal: 1
msrp <> tax_subtotal: 0
freeshipping <> tax_subtotal: -1
discount <> tax_shipping: 1
freeshipping <> tax_shipping: -1
tax_subtotal <> tax_shipping: -1
discount <> tax: -1
tax <> weee: 1
tax_shipping <> weee: 1
freeshipping <> weee: -1
tax_subtotal <> weee: -1

array(11) {
  [0] =>
  string(7) "nominal"
  [1] =>
  string(8) "subtotal"
  [2] =>
  string(8) "shipping"
  [3] =>
  string(11) "grand_total"
  [4] =>
  string(4) "msrp"
  [5] =>
  string(12) "freeshipping"
  [6] =>
  string(12) "tax_subtotal"
  [7] =>
  string(4) "weee"
  [8] =>
  string(12) "tax_shipping"
  [9] =>
  string(8) "discount"
  [10] =>
  string(3) "tax"
}

No PHP5grand_total é o último elemento, mas no PHP7 - não. O problema está relacionado à incerteza da localizaçãomsrp elemento. Achei umpesquisa sobre este assunto associado ao php 5.

Eu resolvi esse problema indicando uma posição relativamsrp. Mas eu me pergunto por que funciona no php5 e não funciona no php7. Esses são os recursos da nova versão do php ou de um bug?

adição nº 1

O problema não é que o PHP7 não sabe classificar elementos iguais, por exemplomsrp egrand_total. Se você olhar para o itemshipping efreeshipping, então eles são claramente definidos, quem deveria ser mais cedo. O PHP5 resolve esse problema, e o PHP7 não.

questionAnswers(2)

yourAnswerToTheQuestion