Hygiene: Deduplicate MenuEntry uniqness check code

The check to verify uniqness of the MenuEntry $name existed
twice, in insert() and in insideAfter() methods. To respect
"do not repeat" yourself rule, we should keep this check
in one place only.

Bug: T221792
Change-Id: I973b0427623e579f97ae47f7c80dc2a9db2fcca7
This commit is contained in:
Piotr Miazga 2019-05-14 11:38:39 +02:00
parent 863f1a7265
commit 6c70498e91

View file

@ -62,6 +62,18 @@ class Group {
return array_map( $entryPresenter, $this->entries );
}
/**
* Helper method to verify that the $name of entry is unique (do not exists
* in current Group )
* @param string $name
* @throws DomainException When the entry already exists
*/
private function throwIfNotUnique( $name ) {
if ( $this->search( $name ) !== -1 ) {
throw new DomainException( "The \"${name}\" entry already exists." );
}
}
/**
* Insert an entry into the menu.
*
@ -71,10 +83,7 @@ class Group {
* @return MenuEntry
*/
public function insert( $name, $isJSOnly = false ) {
if ( $this->search( $name ) !== -1 ) {
throw new DomainException( "The \"${name}\" entry already exists." );
}
$this->throwIfNotUnique( $name );
$this->entries[] = $entry = new MenuEntry( $name, $isJSOnly );
return $entry;
@ -109,9 +118,7 @@ class Group {
* @return MenuEntry
*/
public function insertAfter( $targetName, $name, $isJSOnly = false ) {
if ( $this->search( $name ) !== -1 ) {
throw new DomainException( "The \"${name}\" entry already exists." );
}
$this->throwIfNotUnique( $name );
$index = $this->search( $targetName );