Skip to content

Undefined index during merge #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,39 +380,23 @@ public function merge(self $that): void
continue;
}

if ((\count($lines) > 0) && (\count($this->data[$file]) > 0) && (\count($lines) != \count($this->data[$file]))) {
if (\count($lines) > \count($ours = $this->data[$file])) {
// More lines in the one being added in
$lines = \array_filter(
$lines,
function ($value, $key) use ($ours) {
return \array_key_exists($key, $ours);
},
\ARRAY_FILTER_USE_BOTH
);
} else {
// More lines in the one we currently have
$this->data[$file] = \array_filter(
$this->data[$file],
function ($value, $key) use ($lines) {
return \array_key_exists($key, $lines);
},
\ARRAY_FILTER_USE_BOTH
);
}
}
// we should compare the lines if any of two contains data
$compareLineNumbers = \array_unique(
\array_merge(
\array_keys($this->data[$file]),
\array_keys($that->data[$file])
)
);

foreach ($compareLineNumbers as $line) {
$thatPriority = $this->getLinePriority($that->data[$file], $line);
$thisPriority = $this->getLinePriority($this->data[$file], $line);

foreach ($lines as $line => $data) {
if ($data === null || $this->data[$file][$line] === null) {
// if the line is marked as "dead code" in either, mark it as dead code in the merged result
$this->data[$file][$line] = null;
} elseif (!isset($this->data[$file][$line])) {
// if no data has been set in the current data, overwrite all
$this->data[$file][$line] = $data;
} else {
// otherwise merge data from both coverage files
if ($thatPriority > $thisPriority) {
$this->data[$file][$line] = $that->data[$file][$line];
} elseif ($thatPriority === $thisPriority && \is_array($this->data[$file][$line])) {
$this->data[$file][$line] = \array_unique(
\array_merge($this->data[$file][$line], $data)
\array_merge($this->data[$file][$line], $that->data[$file][$line])
);
}
}
Expand All @@ -422,6 +406,33 @@ function ($value, $key) use ($lines) {
$this->report = null;
}

/**
* Determine the priority for a line
*
* 1 = the line is not set
* 2 = the line has not been tested
* 3 = the line is dead code
* 4 = the line has been tested
*
* During a merge, a higher number is better.
*
* @param array $data
* @param int $line
* @return int
*/
protected function getLinePriority($data, $line)
{
if (!\array_key_exists($line, $data)) {
return 1;
} elseif (\is_array($data[$line]) && \count($data[$line]) === 0) {
return 2;
} elseif ($data[$line] === null) {
return 3;
}

return 4;
}

public function setCacheTokens(bool $flag): void
{
$this->cacheTokens = $flag;
Expand Down
16 changes: 11 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,33 +238,39 @@ protected function getExpectedDataArrayForBankAccount()
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
1 => 'BankAccountTest::testDepositWithdrawMoney'
],
9 => null,
13 => [],
14 => [],
15 => [],
16 => [],
18 => [],
22 => [
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
1 => 'BankAccountTest::testDepositWithdrawMoney'
],
24 => [
0 => 'BankAccountTest::testDepositWithdrawMoney',
],
25 => null,
29 => [
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1 => 'BankAccountTest::testDepositWithdrawMoney'
],
31 => [
0 => 'BankAccountTest::testDepositWithdrawMoney'
],
32 => null
]
];
}

protected function getExpectedDataArrayForBankAccount2()
protected function getExpectedDataArrayForBankAccountInReverseOrder()
{
return [
TEST_FILES_PATH . 'BankAccount.php' => [
8 => [
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
1 => 'BankAccountTest::testDepositWithdrawMoney'
0 => 'BankAccountTest::testDepositWithdrawMoney',
1 => 'BankAccountTest::testBalanceIsInitiallyZero'
],
9 => null,
13 => [],
Expand All @@ -281,8 +287,8 @@ protected function getExpectedDataArrayForBankAccount2()
],
25 => null,
29 => [
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1 => 'BankAccountTest::testDepositWithdrawMoney'
0 => 'BankAccountTest::testDepositWithdrawMoney',
1 => 'BankAccountTest::testBalanceCannotBecomeNegative'
],
31 => [
0 => 'BankAccountTest::testDepositWithdrawMoney'
Expand Down
18 changes: 16 additions & 2 deletions tests/tests/CodeCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace SebastianBergmann\CodeCoverage;

require __DIR__ . '/../_files/BankAccount.php';
require __DIR__ . '/../_files/BankAccountTest.php';

use SebastianBergmann\CodeCoverage\Driver\Driver;
use SebastianBergmann\CodeCoverage\Driver\PHPDBG;
use SebastianBergmann\CodeCoverage\Driver\Xdebug;
Expand Down Expand Up @@ -215,7 +218,7 @@ public function testCollect()
$coverage = $this->getCoverageForBankAccount();

$this->assertEquals(
$this->getExpectedDataArrayForBankAccount2(),
$this->getExpectedDataArrayForBankAccount(),
$coverage->getData()
);

Expand All @@ -241,6 +244,17 @@ public function testMerge()
);
}

public function testMergeReverseOrder()
{
$coverage = $this->getCoverageForBankAccountForLastTwoTests();
$coverage->merge($this->getCoverageForBankAccountForFirstTwoTests());

$this->assertEquals(
$this->getExpectedDataArrayForBankAccountInReverseOrder(),
$coverage->getData()
);
}

public function testMerge2()
{
$coverage = new CodeCoverage(
Expand All @@ -251,7 +265,7 @@ public function testMerge2()
$coverage->merge($this->getCoverageForBankAccount());

$this->assertEquals(
$this->getExpectedDataArrayForBankAccount2(),
$this->getExpectedDataArrayForBankAccount(),
$coverage->getData()
);
}
Expand Down