Rename all methods to fit coding standards #36

Closed
rmccue opened this Issue Dec 2, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@rmccue
Member

rmccue commented Dec 2, 2013

As per core coding standards, all methods should be renamed from semiCamelCase to underscore_case.

This is a backwards compatibility break, so for 0.8-1.0, let's ensure the sCC version is called if it exists (with _deprecated_function calls), but for 1.0, we should remove the BC shim.

@osiux

This comment has been minimized.

Show comment Hide comment
@osiux

osiux Feb 19, 2014

Contributor

I want to help with this, but just to be sure I understand. From something like this:

public function registerRoutes( $routes ) {
    $routes[ $this->base ] = array(
        array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
        array( array( $this, 'newPost' ),  WP_JSON_Server::CREATABLE ),
    );

    $routes[ $this->base . '/(?P<id>\d+)' ] = array(
        array( array( $this, 'getPost' ),    WP_JSON_Server::READABLE ),
        array( array( $this, 'editPost' ),   WP_JSON_Server::EDITABLE ),
        array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
    );
    return $routes;
}

We need to have something like this:

public function registerRoutes( $routes ) {
    _deprecated_function( __CLASS__ . '->' . __METHOD__, '0.9', __CLASS__ . '->register_routes()');

    return $this->register_routes( $routes );
}

public function register_routes( $routes ) {
    $routes[ $this->base ] = array(
        array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
        array( array( $this, 'newPost' ),  WP_JSON_Server::CREATABLE ),
    );

    $routes[ $this->base . '/(?P<id>\d+)' ] = array(
        array( array( $this, 'getPost' ),    WP_JSON_Server::READABLE ),
        array( array( $this, 'editPost' ),   WP_JSON_Server::EDITABLE ),
        array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
    );
    return $routes;
}
Contributor

osiux commented Feb 19, 2014

I want to help with this, but just to be sure I understand. From something like this:

public function registerRoutes( $routes ) {
    $routes[ $this->base ] = array(
        array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
        array( array( $this, 'newPost' ),  WP_JSON_Server::CREATABLE ),
    );

    $routes[ $this->base . '/(?P<id>\d+)' ] = array(
        array( array( $this, 'getPost' ),    WP_JSON_Server::READABLE ),
        array( array( $this, 'editPost' ),   WP_JSON_Server::EDITABLE ),
        array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
    );
    return $routes;
}

We need to have something like this:

public function registerRoutes( $routes ) {
    _deprecated_function( __CLASS__ . '->' . __METHOD__, '0.9', __CLASS__ . '->register_routes()');

    return $this->register_routes( $routes );
}

public function register_routes( $routes ) {
    $routes[ $this->base ] = array(
        array( array( $this, 'getPosts' ), WP_JSON_Server::READABLE ),
        array( array( $this, 'newPost' ),  WP_JSON_Server::CREATABLE ),
    );

    $routes[ $this->base . '/(?P<id>\d+)' ] = array(
        array( array( $this, 'getPost' ),    WP_JSON_Server::READABLE ),
        array( array( $this, 'editPost' ),   WP_JSON_Server::EDITABLE ),
        array( array( $this, 'deletePost' ), WP_JSON_Server::DELETABLE ),
    );
    return $routes;
}
@rmccue

This comment has been minimized.

Show comment Hide comment
@rmccue

rmccue Feb 19, 2014

Member

We need to have something like this:

Correct, but all the getPosts/newPost/etc will need to change too. Here's how we can do that easily:

  1. Rename all the methods
  2. Create a generic __call method that maps from the old names to new names.

Something like...

public function __call($name, $arguments) {
    $underscored = to_underscore( $name );
    if ( method_exists( $this, $underscored ) ) {
        _deprecated_function( __CLASS__ . '->' . $name, '0.9', __CLASS__ . '->' . $underscored );
        return call_user_func_array( array( $this, $underscored ), $arguments );
    }
}

(Where to_underscore needs to actually be implemented too :) )

Member

rmccue commented Feb 19, 2014

We need to have something like this:

Correct, but all the getPosts/newPost/etc will need to change too. Here's how we can do that easily:

  1. Rename all the methods
  2. Create a generic __call method that maps from the old names to new names.

Something like...

public function __call($name, $arguments) {
    $underscored = to_underscore( $name );
    if ( method_exists( $this, $underscored ) ) {
        _deprecated_function( __CLASS__ . '->' . $name, '0.9', __CLASS__ . '->' . $underscored );
        return call_user_func_array( array( $this, $underscored ), $arguments );
    }
}

(Where to_underscore needs to actually be implemented too :) )

@osiux

This comment has been minimized.

Show comment Hide comment
@osiux

osiux Feb 19, 2014

Contributor

Good idea! That can be done. Where should we define that to_underscore function? Maybe a wp=josn-helper.php or something like that? Also, this is only for the 0.9 milestone, right? On 1.0 it should disappear.

Contributor

osiux commented Feb 19, 2014

Good idea! That can be done. Where should we define that to_underscore function? Maybe a wp=josn-helper.php or something like that? Also, this is only for the 0.9 milestone, right? On 1.0 it should disappear.

@rmccue

This comment has been minimized.

Show comment Hide comment
@rmccue

rmccue Feb 19, 2014

Member

Good idea! That can be done. Where should we define that to_underscore function? Maybe a wp=josn-helper.php or something like that?

If we can do it inline, that'd be the preferable option. I think we can do it with a single preg_replace.

Also, this is only for the 0.9 milestone, right? On 1.0 it should disappear.

Yep, it'll only be deprecated for a single release, then removed completely.

Member

rmccue commented Feb 19, 2014

Good idea! That can be done. Where should we define that to_underscore function? Maybe a wp=josn-helper.php or something like that?

If we can do it inline, that'd be the preferable option. I think we can do it with a single preg_replace.

Also, this is only for the 0.9 milestone, right? On 1.0 it should disappear.

Yep, it'll only be deprecated for a single release, then removed completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment