Skip to content

Commit

Permalink
Fix bugs and typos.
Browse files Browse the repository at this point in the history
Bug fixes:
- RegistryKey::getHive() returns invalid type on x64 distributions of php.
- RegistryKey::SubKeyRecursive always results in error.
- RegistryKey::getName() returns half qualified name instead of local keyName.
- RegistryKey::getSubKeyRecursive() returns qualified names for subKeys instead of local names.
  • Loading branch information
DigiLive committed Jun 2, 2020
1 parent ad14c33 commit f2edb65
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
40 changes: 26 additions & 14 deletions src/RegistryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,27 @@ public function __construct(RegistryHandle $handle, float $hive, string $name)
/**
* Get the registry hive the key is located in.
*
* Note: Although a hive is represented as a hex number, which is an int, php will cast this value into a float or
* double because the value causes an overflow.
* The method will return the hive as three different types, defined by the parameter and the 32bit or 64 bit
* version of php.
*
* <pre>
* Parameter value: 32b: Return type:
* false d.c. VARIANT
* true true float
* true false int
* </pre>
*
* @return float Hive of the key.
* @param bool $asNumber True to return the hive as number, False to return the hive as a variant.
*
* @return int|float|VARIANT Hive of the key.
*/
public function getHive(): float
public function getHive($asNumber = true)
{
return $this->hive;
if (!$asNumber) {
return new VARIANT($this->hive, VT_R8);
}

return PHP_INT_SIZE == 4 ? (float)$this->hive : (int)$this->hive;
}

/**
Expand Down Expand Up @@ -150,7 +163,7 @@ public function deleteSubKeyRecursive(string $name)
}

// Delete nested subKeys.
$this->deleteSubKeyRecursive($subKey->getName());
$this->deleteSubKeyRecursive($subKey->getQualifiedName());
}

$this->deleteSubKey($name);
Expand Down Expand Up @@ -249,11 +262,7 @@ public function valueExists(string $name): bool
*/
public function getName(): string
{
if (strpos($this->name, '\\') !== false) {
return substr($this->name, strpos($this->name, '\\') + 1);
}

return $this->name;
return basename($this->name);
}

/**
Expand Down Expand Up @@ -290,13 +299,13 @@ public function getSubKeyRecursive(string $name, bool $includeValues = false): a

// Define the rootKey properties.
$returnValue['type'] = 'key';
$returnValue['name'] = $currentKey->getName();
$returnValue['name'] = $name;
$returnValue['keys'] = [];

// Get the rootKey's subKeys.
foreach ($currentKey->getSubKeyIterator() as $subKey) {
// Get nested sub keys and values.
$returnValue['keys'][] = $this->getSubKeyRecursive($subKey->getQualifiedName(), $includeValues);
$returnValue['keys'][] = $currentKey->getSubKeyRecursive($subKey->getName(), $includeValues);
}

if ($includeValues) {
Expand All @@ -314,7 +323,10 @@ public function getSubKeyRecursive(string $name, bool $includeValues = false): a
}

/**
* Get the fully-qualified name of the key.
* Get the qualified name of the key.
*
* The name includes:
* - All of the keyNames in the hierarchic sequence above this key and the name of the key itself.
*
* @return string Name of the key.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RegistryKeyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function rewind()

// Enumerate subKeys.
$errorCode = $this->handle->enumKey(
$this->registryKey->getHive(),
$this->registryKey->getHive(false),
$this->registryKey->getQualifiedName(),
$this->subKeyNames
);
Expand Down
2 changes: 1 addition & 1 deletion src/RegistryValueIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function rewind()

// Attempt to enumerate values.
$errorCode = $this->handle->enumValues(
$this->registryKey->getHive(),
$this->registryKey->getHive(false),
$this->registryKey->getQualifiedName(),
$this->valueNames,
$this->valueTypes
Expand Down
2 changes: 1 addition & 1 deletion tests/RegistryKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public function testGetSubKeyIterator()
/**
* Test getting the type of a value.
*
* Note: This test doesn't use the mocked registry handle, but instantiate a real one!
* Note: This test doesn't use the mocked registry handle, but instantiates a real one!
* It will actually read from the registry.
*/
public function testGetValueType()
Expand Down

0 comments on commit f2edb65

Please sign in to comment.