Skip to content

Commit

Permalink
Add PhutilInvalidStateException class
Browse files Browse the repository at this point in the history
Summary: There's a bunch of `Call %s before calling %s!` exceptions that can be simplified with a custom script.

Test Plan: Wrote unit tests.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D12803
  • Loading branch information
joshuaspence committed May 13, 2015
1 parent 211230c commit f06c0b7
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 58 deletions.
4 changes: 4 additions & 0 deletions src/__phutil_library_map__.php
Expand Up @@ -190,6 +190,8 @@
'PhutilInRequestKeyValueCache' => 'cache/PhutilInRequestKeyValueCache.php',
'PhutilInteractiveEditor' => 'console/PhutilInteractiveEditor.php',
'PhutilInvalidRuleParserGeneratorException' => 'parser/generator/exception/PhutilInvalidRuleParserGeneratorException.php',
'PhutilInvalidStateException' => 'exception/PhutilInvalidStateException.php',
'PhutilInvalidStateExceptionTestCase' => 'exception/__tests__/PhutilInvalidStateExceptionTestCase.php',
'PhutilInvisibleSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilInvisibleSyntaxHighlighter.php',
'PhutilIrreducibleRuleParserGeneratorException' => 'parser/generator/exception/PhutilIrreducibleRuleParserGeneratorException.php',
'PhutilJIRAAuthAdapter' => 'auth/PhutilJIRAAuthAdapter.php',
Expand Down Expand Up @@ -621,6 +623,8 @@
'PhutilIPAddressTestCase' => 'PhutilTestCase',
'PhutilInRequestKeyValueCache' => 'PhutilKeyValueCache',
'PhutilInvalidRuleParserGeneratorException' => 'PhutilParserGeneratorException',
'PhutilInvalidStateException' => 'Exception',
'PhutilInvalidStateExceptionTestCase' => 'PhutilTestCase',
'PhutilIrreducibleRuleParserGeneratorException' => 'PhutilParserGeneratorException',
'PhutilJIRAAuthAdapter' => 'PhutilOAuth1AuthAdapter',
'PhutilJSONParserException' => 'Exception',
Expand Down
3 changes: 1 addition & 2 deletions src/auth/PhutilOAuthAuthAdapter.php
Expand Up @@ -151,8 +151,7 @@ protected function loadRefreshTokenData($refresh_token) {
protected function loadAccessTokenData() {
$code = $this->getCode();
if (!$code) {
throw new Exception(
pht('Call %s before accessing adapter information.', 'setCode()'));
throw new PhutilInvalidStateException('setCode');
}

$params = array(
Expand Down
11 changes: 2 additions & 9 deletions src/cache/PhutilDirectoryKeyValueCache.php
Expand Up @@ -169,10 +169,7 @@ public function setCacheDirectory($directory) {
*/
private function getCacheDirectory() {
if (!$this->cacheDirectory) {
throw new Exception(
pht(
'Call %s before using a directory cache!',
'setCacheDirectory()'));
throw new PhutilInvalidStateException('setCacheDirectory');
}
return $this->cacheDirectory;
}
Expand Down Expand Up @@ -237,11 +234,7 @@ private function lockCache($wait = 0) {
*/
private function unlockCache() {
if (!$this->lock) {
throw new Exception(
pht(
'Call %s before %s!',
'lockCache()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('lockCache');
}

$this->lock->unlock();
Expand Down
5 changes: 1 addition & 4 deletions src/cache/PhutilMemcacheKeyValueCache.php
Expand Up @@ -114,10 +114,7 @@ private function bucketKeys(array $keys) {
$n = count($this->servers);

if (!$n) {
throw new Exception(
pht(
'Call %s before using Memcache!',
'setServers()'));
throw new PhutilInvalidStateException('setServers');
}

foreach ($keys as $key) {
Expand Down
11 changes: 2 additions & 9 deletions src/cache/PhutilOnDiskKeyValueCache.php
Expand Up @@ -177,11 +177,7 @@ private function loadCache($hold_lock) {
*/
private function saveCache() {
if (!$this->lock) {
throw new Exception(
pht(
'Call %s before %s!',
'loadCache($hold_lock=true)',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('loadCache');
}

// We're holding a lock so we're safe to do a write to a well-known file.
Expand All @@ -201,10 +197,7 @@ private function saveCache() {
*/
private function getCacheFile() {
if (!$this->cacheFile) {
throw new Exception(
pht(
'Call %s before using a disk cache!',
'setCacheFile()'));
throw new PhutilInvalidStateException('setCacheFile');
}
return $this->cacheFile;
}
Expand Down
30 changes: 30 additions & 0 deletions src/exception/PhutilInvalidStateException.php
@@ -0,0 +1,30 @@
<?php

final class PhutilInvalidStateException extends Exception {
private $callee;
private $function;

public function __construct($function, $callee = null) {
if ($callee === null) {
$callee = idx(debug_backtrace(), 1);
$callee = idx($callee, 'function');
}

$this->callee = $callee;
$this->function = $function;

parent::__construct(
pht(
'Call %s before calling %s!',
$this->function.'()',
$this->callee.'()'));
}

public function getCallee() {
return $this->callee;
}

public function getFunction() {
return $this->function;
}
}
17 changes: 17 additions & 0 deletions src/exception/__tests__/PhutilInvalidStateExceptionTestCase.php
@@ -0,0 +1,17 @@
<?php

final class PhutilInvalidStateExceptionTestCase extends PhutilTestCase {

public function testException() {
try {
throw new PhutilInvalidStateException('someMethod');
} catch (PhutilInvalidStateException $ex) {
$this->assertEqual(
__FUNCTION__,
$ex->getCallee());
$this->assertEqual(
'someMethod',
$ex->getFunction());
}
}
}
36 changes: 6 additions & 30 deletions src/parser/PhutilParserGenerator.php
Expand Up @@ -118,44 +118,28 @@ public function getStartRule() {

public function getEOFSymbol() {
if ($this->eofSymbol === null) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->eofSymbol;
}

public function getInitSymbol() {
if ($this->initSymbol === null) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->initSymbol;
}

public function getEpsilonSymbol() {
if ($this->epsilonSymbol === null) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->epsilonSymbol;
}

public function getEndSymbol() {
if ($this->endSymbol === null) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->endSymbol;
}
Expand Down Expand Up @@ -905,11 +889,7 @@ public static function parseTokensWithTables(
*/
public function inspectRules() {
if (!$this->rulesValidated) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->rules;
}
Expand All @@ -920,11 +900,7 @@ public function inspectRules() {
*/
public function inspectFirstTable() {
if ($this->firstTable === null) {
throw new Exception(
pht(
'Call %s before %s!',
'processGrammar()',
__FUNCTION__.'()'));
throw new PhutilInvalidStateException('processGrammar');
}
return $this->firstTable;
}
Expand Down
6 changes: 2 additions & 4 deletions src/utils/PhutilEditDistanceMatrix.php
Expand Up @@ -165,8 +165,7 @@ public function setSequences(array $x, array $y) {

private function requireSequences() {
if ($this->x === null) {
throw new Exception(
pht('Call %s before performing useful work!', 'setSequences()'));
throw new PhutilInvalidStateException('setSequences');
}
}

Expand Down Expand Up @@ -292,8 +291,7 @@ private function padEditString($str) {

private function getTypeMatrix() {
if (!$this->computeString) {
throw new Exception(
pht('Call %s before %s.', 'setComputeString()', 'getTypeMatrix()'));
throw new PhutilInvalidStateException('setComputeString');
}
if ($this->typeMatrix === null) {
$this->computeMatrix($this->x, $this->y);
Expand Down

0 comments on commit f06c0b7

Please sign in to comment.