end( $catPath )->getName() doesn't work when you want to compare the result of end(). This has to be separated